[issue6550] asyncore incorrect failure when connection is refused and using async_chat channel

Nir Soffer report at bugs.python.org
Thu Jul 23 00:42:51 CEST 2009


New submission from Nir Soffer <nirsof at gmail.com>:

When using asynchat.async_chat channel, and connection is refused, 
asyncore fail incorrectly.

First, instead of ECONNREFUSED, you get EPIPE, second, you also get a 
EBADF exception, and finally, the channel handle_close is called  twice.

The problem is the way asyncore detect a connect event. On the first 
read event, asyncore set the connected flag to true, and invoke the 
channel handle_connect method. Typically, the channel will try to push 
something to the remote endpoint at this point. Since "connected" is 
true, push() will try to send to the socket, and fail with EPIPE, 
because the socket is not connected.

The socket error in push will invoke the channel handle_error, which 
typically will call handle_close to close the socket. 
handle_connect_event will return without any error, because the error 
was handled inside push.

Now asyncore will invoke the channel handle_read method, which fails 
with bad file descriptor, since the channel closed the socket on the 
previous error.

How should this work correctly?

1. We want to get a connection refused error in this case
2. The failure should cause only single exception
3. The channel should be close once

I added these test to test_asyncore.py, and all of them fail with 
current code.

Turns out that single line change fix all the failing tests: set the 
"connected" state after the call to handle_connect, just like it used to 
be in 2.5.

With this change, when the channel try to push something to the remote 
endpoint, it will keep it in the fifo, since connected is false. This is 
also correct, since we will know that we are connected only after 
handle_read is called. In handle_read, we fail with ECONNREFUSED and 
close the channel.

The fix is tested currently only on Mac OS X 10.5. I have seen this 
issue also on Ubuntu 9.04.

----------
components: Library (Lib)
files: asyncore-handle-connect-event.patch
keywords: patch
messages: 90830
nosy: nirs
severity: normal
status: open
title: asyncore incorrect failure when connection is refused and using async_chat channel
type: behavior
versions: Python 2.6
Added file: http://bugs.python.org/file14543/asyncore-handle-connect-event.patch

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


More information about the Python-bugs-list mailing list