Skip to content

Commit a0e3d2d

Browse files
fafhrd911st1
authored andcommitted
Closing transport during handshake process leaks socket (#480)
1 parent b4e5fee commit a0e3d2d

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

Lib/asyncio/sslproto.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,8 +546,11 @@ def _get_extra_info(self, name, default=None):
546546
def _start_shutdown(self):
547547
if self._in_shutdown:
548548
return
549-
self._in_shutdown = True
550-
self._write_appdata(b'')
549+
if self._in_handshake:
550+
self._abort()
551+
else:
552+
self._in_shutdown = True
553+
self._write_appdata(b'')
551554

552555
def _write_appdata(self, data):
553556
self._write_backlog.append((data, 0))

Lib/test/test_asyncio/test_sslproto.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def mock_handshake(callback):
4242
sslpipe.do_handshake.side_effect = mock_handshake
4343
with mock.patch('asyncio.sslproto._SSLPipe', return_value=sslpipe):
4444
ssl_proto.connection_made(transport)
45+
return transport
4546

4647
def test_cancel_handshake(self):
4748
# Python issue #23197: cancelling a handshake must not raise an
@@ -95,6 +96,20 @@ def test_connection_lost(self):
9596
test_utils.run_briefly(self.loop)
9697
self.assertIsInstance(waiter.exception(), ConnectionAbortedError)
9798

99+
def test_close_during_handshake(self):
100+
# bpo-29743 Closing transport during handshake process leaks socket
101+
waiter = asyncio.Future(loop=self.loop)
102+
ssl_proto = self.ssl_protocol(waiter)
103+
104+
def do_handshake(callback):
105+
return []
106+
107+
transport = self.connection_made(ssl_proto)
108+
test_utils.run_briefly(self.loop)
109+
110+
ssl_proto._app_transport.close()
111+
self.assertTrue(transport.abort.called)
112+
98113
def test_get_extra_info_on_closed_connection(self):
99114
waiter = asyncio.Future(loop=self.loop)
100115
ssl_proto = self.ssl_protocol(waiter)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,9 @@ Extension Modules
350350
Library
351351
-------
352352

353+
- bpo-29743: Closing transport during handshake process leaks open socket.
354+
Patch by Nikolay Kim
355+
353356
- bpo-27585: Fix waiter cancellation in asyncio.Lock.
354357
Patch by Mathieu Sornay.
355358

0 commit comments

Comments
 (0)