Skip to content

Commit 64a68c3

Browse files
bpo-43253: Don't call shutdown() for invalid socket handles (GH-31892)
(cherry picked from commit 7015541) Co-authored-by: Maximilian Hils <[email protected]>
1 parent 91dfa91 commit 64a68c3

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
@@ -237,6 +237,14 @@ def test_close_buffer(self):
237237
test_utils.run_briefly(self.loop)
238238
self.assertFalse(self.protocol.connection_lost.called)
239239

240+
def test_close_invalid_sockobj(self):
241+
tr = self.socket_transport()
242+
self.sock.fileno.return_value = -1
243+
tr.close()
244+
test_utils.run_briefly(self.loop)
245+
self.protocol.connection_lost.assert_called_with(None)
246+
self.assertFalse(self.sock.shutdown.called)
247+
240248
@mock.patch('asyncio.base_events.logger')
241249
def test_fatal_error(self, m_logging):
242250
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)