]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43253: Don't call shutdown() for invalid socket handles (GH-31892)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 15 Mar 2022 15:23:47 +0000 (08:23 -0700)
committerGitHub <noreply@github.com>
Tue, 15 Mar 2022 15:23:47 +0000 (08:23 -0700)
(cherry picked from commit 70155412f1543f100d4aa309b8691cbcabd3e0e1)

Co-authored-by: Maximilian Hils <git@maximilianhils.com>
Lib/asyncio/proactor_events.py
Lib/test/test_asyncio/test_proactor_events.py
Misc/NEWS.d/next/Library/2022-03-15-07-53-45.bpo-43253.rjdLFj.rst [new file with mode: 0644]

index 6762253abc967826789a061caeabe7e32294e7fe..e3f95cf21d6179bd0188e185b4dbeb3ee76eb4b2 100644 (file)
@@ -158,7 +158,7 @@ class _ProactorBasePipeTransport(transports._FlowControlMixin,
             # 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
index 1bbb487626e89e35189bf34e9a8f00349439087f..acf21494823ae8d84a395706420bd7db889aa1f5 100644 (file)
@@ -237,6 +237,14 @@ class ProactorSocketTransportTests(test_utils.TestCase):
         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 (file)
index 0000000..b9920cb
--- /dev/null
@@ -0,0 +1 @@
+Fix a crash when closing transports where the underlying socket handle is already invalid on the Proactor event loop.