Skip to content

Commit 420ad08

Browse files
committed
Errors from socket.connect() should be treated the same way as async failures.
On freebsd non-blocking connect() may return certain errors (such as ECONNREFUSED from localhost) immediately instead of returning EINPROGRESS and then giving the error later. Also improve the test for ipv6 compatibility, since freebsd returns a different error than other platforms when ipv6 is not available.
1 parent 4b346bd commit 420ad08

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

tornado/iostream.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,18 @@ def connect(self, address, callback=None):
119119
try:
120120
self.socket.connect(address)
121121
except socket.error, e:
122-
# In non-blocking mode connect() always raises an exception
122+
# In non-blocking mode we expect connect() to raise an
123+
# exception with EINPROGRESS or EWOULDBLOCK.
124+
#
125+
# On freebsd, other errors such as ECONNREFUSED may be
126+
# returned immediately when attempting to connect to
127+
# localhost, so handle them the same way as an error
128+
# reported later in _handle_connect.
123129
if e.args[0] not in (errno.EINPROGRESS, errno.EWOULDBLOCK):
124-
raise
130+
logging.warning("Connect error on fd %d: %s",
131+
self.socket.fileno(), e)
132+
self.close()
133+
return
125134
self._connect_callback = stack_context.wrap(callback)
126135
self._add_io_state(self.io_loop.WRITE)
127136

tornado/test/simple_httpclient_test.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,15 @@ def test_request_timeout(self):
141141
self.assertEqual(str(response.error), "HTTP 599: Timeout")
142142

143143
def test_ipv6(self):
144+
if not socket.has_ipv6:
145+
# python compiled without ipv6 support, so skip this test
146+
return
144147
try:
145148
self.http_server.listen(self.get_http_port(), address='::1')
146149
except socket.gaierror, e:
147150
if e.errno == socket.EAI_ADDRFAMILY:
148-
# ipv6 is not configured on this system, so skip this test
151+
# python supports ipv6, but it's not configured on the network
152+
# interface, so skip this test.
149153
return
150154
raise
151155
url = self.get_url("/hello").replace("localhost", "[::1]")

0 commit comments

Comments
 (0)