Skip to content

[3.10] bpo-43253: Don't call shutdown() for invalid socket handles (GH-31892) #31906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/asyncio/proactor_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def _call_connection_lost(self, exc):
# end then it may fail with ERROR_NETNAME_DELETED if we
# just close our end. First calling shutdown() seems to
# cure it, but maybe using DisconnectEx() would be better.
if hasattr(self._sock, 'shutdown'):
if hasattr(self._sock, 'shutdown') and self._sock.fileno() != -1:
self._sock.shutdown(socket.SHUT_RDWR)
self._sock.close()
self._sock = None
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_asyncio/test_proactor_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,14 @@ def test_close_buffer(self):
test_utils.run_briefly(self.loop)
self.assertFalse(self.protocol.connection_lost.called)

def test_close_invalid_sockobj(self):
tr = self.socket_transport()
self.sock.fileno.return_value = -1
tr.close()
test_utils.run_briefly(self.loop)
self.protocol.connection_lost.assert_called_with(None)
self.assertFalse(self.sock.shutdown.called)

@mock.patch('asyncio.base_events.logger')
def test_fatal_error(self, m_logging):
tr = self.socket_transport()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a crash when closing transports where the underlying socket handle is already invalid on the Proactor event loop.