Skip to content

Commit 30378df

Browse files
committed
Make sure connection_lost is called when connection_made is errored out
1 parent 87d4f11 commit 30378df

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

tests/test_tcp.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ def test_tcp_handle_exception_in_connection_made(self):
515515
self.loop.set_exception_handler(lambda *args: None)
516516

517517
fut = asyncio.Future(loop=self.loop)
518+
connection_lost_called = asyncio.Future(loop=self.loop)
518519

519520
async def server(reader, writer):
520521
try:
@@ -526,6 +527,9 @@ class Proto(asyncio.Protocol):
526527
def connection_made(self, tr):
527528
1 / 0
528529

530+
def connection_lost(self, exc):
531+
connection_lost_called.set_result(exc)
532+
529533
srv = self.loop.run_until_complete(asyncio.start_server(
530534
server,
531535
'127.0.0.1', 0,
@@ -545,6 +549,9 @@ async def runner():
545549
self.loop.run_until_complete(srv.wait_closed())
546550
self.loop.run_until_complete(fut)
547551

552+
self.assertIsNone(
553+
self.loop.run_until_complete(connection_lost_called))
554+
548555

549556
class Test_UV_TCP(_TestTCP, tb.UVTestCase):
550557

uvloop/handles/basetransport.pyx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,17 @@ cdef class UVBaseTransport(UVSocketHandle):
128128

129129
_loop_ready_len = self._loop._ready_len
130130

131+
# Set _protocol_connected to 1 before calling "connection_made":
132+
# if transport is aborted or closed, "connection_lost" will
133+
# still be scheduled.
134+
self._protocol_connected = 1
135+
131136
try:
132137
self._protocol.connection_made(self)
133138
except:
134139
self._wakeup_waiter()
135140
raise
136141

137-
self._protocol_connected = 1
138-
139142
if self._closing:
140143
# This might happen when "transport.abort()" is called
141144
# from "Protocol.connection_made".

0 commit comments

Comments
 (0)