Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ff9511b

Browse files
authored
Closing transport during handshake process leaks socket (#480) (#2045)
1 parent a033804 commit ff9511b

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
@@ -550,8 +550,11 @@ def _get_extra_info(self, name, default=None):
550550
def _start_shutdown(self):
551551
if self._in_shutdown:
552552
return
553-
self._in_shutdown = True
554-
self._write_appdata(b'')
553+
if self._in_handshake:
554+
self._abort()
555+
else:
556+
self._in_shutdown = True
557+
self._write_appdata(b'')
555558

556559
def _write_appdata(self, data):
557560
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
@@ -56,6 +56,9 @@ Extension Modules
5656
Library
5757
-------
5858

59+
- bpo-29743: Closing transport during handshake process leaks open socket.
60+
Patch by Nikolay Kim
61+
5962
- bpo-27585: Fix waiter cancellation in asyncio.Lock.
6063
Patch by Mathieu Sornay.
6164

0 commit comments

Comments
 (0)