[New-bugs-announce] [issue4690] asyncore calls handle_write() on closed sockets when use_poll=True

Forest Wilkinson report at bugs.python.org
Thu Dec 18 01:32:02 CET 2008


New submission from Forest Wilkinson <forest at users.sourceforge.net>:

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.

----------
components: Library (Lib)
files: pollwritefail.py
messages: 78003
nosy: forest
severity: normal
status: open
title: asyncore calls handle_write() on closed sockets when use_poll=True
type: behavior
versions: Python 2.5, Python 2.5.3, Python 2.6
Added file: http://bugs.python.org/file12390/pollwritefail.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue4690>
_______________________________________


More information about the New-bugs-announce mailing list