Skip to content

Commit 5832655

Browse files
fix: do not abort client conn transport on close
1 parent 48a3c0d commit 5832655

File tree

3 files changed

+4
-35
lines changed

3 files changed

+4
-35
lines changed

Lib/asyncio/proactor_events.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,8 @@ def __init__(self, loop, sock, protocol, waiter=None,
6262
self._closing = False # Set when close() called.
6363
self._called_connection_lost = False
6464
self._eof_written = False
65-
if self._server is not None:
66-
if self._server.is_serving():
67-
self._server._attach(self)
68-
else:
69-
self.abort()
70-
return
65+
if self._server is not None and self._server.is_serving():
66+
self._server._attach(self)
7167
self._loop.call_soon(self._protocol.connection_made, self)
7268
if waiter is not None:
7369
# only wake up the waiter when connection_made() has been called

Lib/asyncio/selector_events.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -794,12 +794,8 @@ def __init__(self, loop, sock, protocol, extra=None, server=None):
794794
self._closing = False # Set when close() called.
795795
self._paused = False # Set when pause_reading() called
796796

797-
if self._server is not None:
798-
if self._server.is_serving():
799-
self._server._attach(self)
800-
else:
801-
self.abort()
802-
return
797+
if self._server is not None and self._server.is_serving():
798+
self._server._attach(self)
803799

804800
loop._transports[self._sock_fd] = self
805801

Lib/test/test_asyncio/test_server.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -272,29 +272,6 @@ async def serve(rd, wr):
272272
await asyncio.sleep(0)
273273
self.assertTrue(task.done())
274274

275-
async def test_close_before_transport_attach(self):
276-
proto = Mock()
277-
loop = asyncio.get_running_loop()
278-
srv = await loop.create_server(lambda *_: proto, socket_helper.HOSTv4, 0)
279-
280-
await srv.start_serving()
281-
addr = srv.sockets[0].getsockname()
282-
283-
# Create a connection to the server but close the server before the
284-
# socket transport for the connection is created and attached
285-
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
286-
s.connect(addr)
287-
await asyncio.sleep(0) # loop select reader
288-
await asyncio.sleep(0) # accept conn 1
289-
srv.close()
290-
291-
# Ensure the protocol is given an opportunity to handle this event
292-
# gh109564: the transport would be unclosed and will cause a loop
293-
# exception due to a double-call to Server._wakeup
294-
await asyncio.sleep(0)
295-
await asyncio.sleep(0)
296-
proto.connection_lost.assert_called()
297-
298275

299276
# Test the various corner cases of Unix server socket removal
300277
class UnixServerCleanupTests(unittest.IsolatedAsyncioTestCase):

0 commit comments

Comments
 (0)