classification
Title: asyncore calls handle_write() on closed sockets when use_poll=True
Type: behavior Stage: needs patch
Components: Library (Lib) Versions: Python 3.1, Python 3.2, Python 2.7, Python 2.6
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: giampaolo.rodola Nosy List: forest, giampaolo.rodola, intgr, josiah.carlson
Priority: normal Keywords:

Created on 2008-12-18 00:32 by forest, last changed 2010-08-04 09:58 by giampaolo.rodola. This issue is now closed.

Files
File name Uploaded Description Edit
pollwritefail.py forest, 2008-12-18 00:31 example code
Messages (2)
msg78003 - (view) Author: Forest (forest) Date: 2008-12-18 00:31
With use_poll=True on linux, asyncore calls handle_write() after the
socket has been closed.

More specifically, it looks like asyncore dispatches handle_read() and
handle_close() events between the writable() test and the corresponding
handle_write() call.  If handle_close() calls close(), as asyncore's
default implementation does, the subsequent handle_write() will fail and
generate an EBADF (bad file descriptor) exception.  If handle_error()
also calls close(), as asyncore's default implementation does, this will
mean close() gets called twice on the same socket.

I am attaching example code which demonstrates the problem on Linux
2.6.24 using python 2.5.2, 2.5.3rc1, and 2.6.  In one window, run
pollwritefail.py.  In another window, establish a TCP connection to port
12345 and immediately close it without reading or writing.  This can be
done from within the python interactive interpreter like this:

  import socket
  s=socket.socket( socket.AF_INET, socket.SOCK_STREAM); s.connect(
    ('localhost', 12345)); s.close()

The output from pollwritefail.py will look like this:

  writable() - asyncore asked if we have data to write
  handle_read() - asyncore asked us to read
  handle_close() - asyncore said the remote host closed connection
  close() - we are closing our end of the connection
  handle_write() - asyncore asked us to write
  handle_error() - asyncore exception: (9, 'Bad file descriptor')
  close() - we are closing our end of the connection

IMHO, two things need fixing here:

1. When writable() returns True, the next handler asyncore calls should
be handle_write().  Calling other handlers in between risks invalidating
the writable() return value.

2. After close(), asyncore should not call handle_write(), even if
writable() would return true.
msg112780 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2010-08-04 09:58
This problem must have been solved at some point because this is what I get now on Linux by using python 2.7:

writable() - asyncore asked if we have data to write
handle_read() - asyncore asked us to read
handle_close() - asyncore said the remote host closed connection
close() - we are closing our end of the connection
handle_close() - asyncore said the remote host closed connection
close() - we are closing our end of the connection

Closing out as outdated.
History
Date User Action Args
2010-08-04 09:58:34giampaolo.rodolasetstatus: open -> closed
resolution: out of date
messages: + msg112780

versions: + Python 2.6
2010-07-09 20:33:01giampaolo.rodolasetassignee: giampaolo.rodola
2010-07-09 17:29:09BreamoreBoysetstage: needs patch
versions: + Python 3.1, Python 2.7, Python 3.2, - Python 2.6
2009-03-24 17:22:19intgrsetnosy: + intgr
2008-12-20 14:36:03loewissetversions: - Python 2.5, Python 2.5.3
2008-12-18 14:31:54giampaolo.rodolasetnosy: + giampaolo.rodola, josiah.carlson
2008-12-18 00:32:01forestcreate