]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-113214: Fix SSLProto exception handling in SSL-over-SSL scenarios (#113334)
authorMartijn Pieters <mj@zopatista.com>
Wed, 20 Dec 2023 23:09:01 +0000 (23:09 +0000)
committerGitHub <noreply@github.com>
Wed, 20 Dec 2023 23:09:01 +0000 (15:09 -0800)
When wrapped, `_SSLProtocolTransport._force_close(exc)` is called just like in the unwrapped scenario `_SelectorTransport._force_close(exc)` or `_ProactorBasePipeTransport._force_close(exc)` would be called, except here the exception needs to be passed through the `SSLProtocol._abort()` method, which didn't accept an exception object.

This commit ensures that this path works, in the same way that the uvloop implementation of SSLProto passes on the exception (on which the current implementation of SSLProto is based).

Lib/asyncio/sslproto.py
Lib/test/test_asyncio/test_sslproto.py
Misc/NEWS.d/next/Library/2023-12-20-21-18-51.gh-issue-113214.JcV9Mn.rst [new file with mode: 0644]

index 3eb65a8a08b5a05667995a7f67824d49ad46205b..cbb6527d0b28e0de3c6fba8bcd28e21075ce69b3 100644 (file)
@@ -243,13 +243,12 @@ class _SSLProtocolTransport(transports._FlowControlMixin,
         The protocol's connection_lost() method will (eventually) be
         called with None as its argument.
         """
-        self._closed = True
-        if self._ssl_protocol is not None:
-            self._ssl_protocol._abort()
+        self._force_close(None)
 
     def _force_close(self, exc):
         self._closed = True
-        self._ssl_protocol._abort(exc)
+        if self._ssl_protocol is not None:
+            self._ssl_protocol._abort(exc)
 
     def _test__append_write_backlog(self, data):
         # for test only
@@ -614,7 +613,7 @@ class SSLProtocol(protocols.BufferedProtocol):
         if self._app_transport is not None:
             self._app_transport._closed = True
         if self._state == SSLProtocolState.DO_HANDSHAKE:
-            self._abort()
+            self._abort(None)
         else:
             self._set_state(SSLProtocolState.FLUSHING)
             self._shutdown_timeout_handle = self._loop.call_later(
@@ -661,10 +660,10 @@ class SSLProtocol(protocols.BufferedProtocol):
         else:
             self._loop.call_soon(self._transport.close)
 
-    def _abort(self):
+    def _abort(self, exc):
         self._set_state(SSLProtocolState.UNWRAPPED)
         if self._transport is not None:
-            self._transport.abort()
+            self._transport._force_close(exc)
 
     # Outgoing flow
 
index 37d015339761c645c729b86861ca262606a95218..f5f0afeab51c9e9de865481ac2b6b3a585070353 100644 (file)
@@ -47,6 +47,7 @@ class SslProtoHandshakeTests(test_utils.TestCase):
         sslobj = mock.Mock()
         # emulate reading decompressed data
         sslobj.read.side_effect = ssl.SSLWantReadError
+        sslobj.write.side_effect = ssl.SSLWantReadError
         if do_handshake is not None:
             sslobj.do_handshake = do_handshake
         ssl_proto._sslobj = sslobj
@@ -120,7 +121,19 @@ class SslProtoHandshakeTests(test_utils.TestCase):
         test_utils.run_briefly(self.loop)
 
         ssl_proto._app_transport.close()
-        self.assertTrue(transport.abort.called)
+        self.assertTrue(transport._force_close.called)
+
+    def test_close_during_ssl_over_ssl(self):
+        # gh-113214: passing exceptions from the inner wrapped SSL protocol to the
+        # shim transport provided by the outer SSL protocol should not raise
+        # attribute errors
+        outer = self.ssl_protocol(proto=self.ssl_protocol())
+        self.connection_made(outer)
+        # Closing the outer app transport should not raise an exception
+        messages = []
+        self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))
+        outer._app_transport.close()
+        self.assertEqual(messages, [])
 
     def test_get_extra_info_on_closed_connection(self):
         waiter = self.loop.create_future()
diff --git a/Misc/NEWS.d/next/Library/2023-12-20-21-18-51.gh-issue-113214.JcV9Mn.rst b/Misc/NEWS.d/next/Library/2023-12-20-21-18-51.gh-issue-113214.JcV9Mn.rst
new file mode 100644 (file)
index 0000000..6db74cd
--- /dev/null
@@ -0,0 +1 @@
+Fix an ``AttributeError`` during asyncio SSL protocol aborts in SSL-over-SSL scenarios.