[Python-checkins] bpo-43253: Don't call shutdown() for invalid socket handles (GH-31892)
miss-islington
webhook-mailer at python.org
Tue Mar 15 11:22:10 EDT 2022
https://github.com/python/cpython/commit/88c243fd8d5a43282ef06bd0872e3b88c68bb856
commit: 88c243fd8d5a43282ef06bd0872e3b88c68bb856
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-03-15T08:22:01-07:00
summary:
bpo-43253: Don't call shutdown() for invalid socket handles (GH-31892)
(cherry picked from commit 70155412f1543f100d4aa309b8691cbcabd3e0e1)
Co-authored-by: Maximilian Hils <git at maximilianhils.com>
files:
A Misc/NEWS.d/next/Library/2022-03-15-07-53-45.bpo-43253.rjdLFj.rst
M Lib/asyncio/proactor_events.py
M Lib/test/test_asyncio/test_proactor_events.py
diff --git a/Lib/asyncio/proactor_events.py b/Lib/asyncio/proactor_events.py
index e551896395b84..411685bbda55e 100644
--- a/Lib/asyncio/proactor_events.py
+++ b/Lib/asyncio/proactor_events.py
@@ -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
diff --git a/Lib/test/test_asyncio/test_proactor_events.py b/Lib/test/test_asyncio/test_proactor_events.py
index c024477e3baba..fc6ee1c1c423e 100644
--- a/Lib/test/test_asyncio/test_proactor_events.py
+++ b/Lib/test/test_asyncio/test_proactor_events.py
@@ -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()
diff --git a/Misc/NEWS.d/next/Library/2022-03-15-07-53-45.bpo-43253.rjdLFj.rst b/Misc/NEWS.d/next/Library/2022-03-15-07-53-45.bpo-43253.rjdLFj.rst
new file mode 100644
index 0000000000000..b9920cb821b35
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-03-15-07-53-45.bpo-43253.rjdLFj.rst
@@ -0,0 +1 @@
+Fix a crash when closing transports where the underlying socket handle is already invalid on the Proactor event loop.
More information about the Python-checkins
mailing list