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

Skip to content

Commit 7015541

Browse files
authored
bpo-43253: Don't call shutdown() for invalid socket handles (pythonGH-31892)
1 parent 6dfe09f commit 7015541

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

Lib/asyncio/proactor_events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def _call_connection_lost(self, exc):
158158
# end then it may fail with ERROR_NETNAME_DELETED if we
159159
# just close our end. First calling shutdown() seems to
160160
# cure it, but maybe using DisconnectEx() would be better.
161-
if hasattr(self._sock, 'shutdown'):
161+
if hasattr(self._sock, 'shutdown') and self._sock.fileno() != -1:
162162
self._sock.shutdown(socket.SHUT_RDWR)
163163
self._sock.close()
164164
self._sock = None

Lib/test/test_asyncio/test_proactor_events.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,14 @@ def test_close_buffer(self):
242242
test_utils.run_briefly(self.loop)
243243
self.assertFalse(self.protocol.connection_lost.called)
244244

245+
def test_close_invalid_sockobj(self):
246+
tr = self.socket_transport()
247+
self.sock.fileno.return_value = -1
248+
tr.close()
249+
test_utils.run_briefly(self.loop)
250+
self.protocol.connection_lost.assert_called_with(None)
251+
self.assertFalse(self.sock.shutdown.called)
252+
245253
@mock.patch('asyncio.base_events.logger')
246254
def test_fatal_error(self, m_logging):
247255
tr = self.socket_transport()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a crash when closing transports where the underlying socket handle is already invalid on the Proactor event loop.

0 commit comments

Comments
 (0)