mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
[3.12] GH-113214: Fix SSLProto exception handling in SSL-over-SSL scenarios (GH-113334) (#113339)
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).
(cherry picked from commit 1ff0238594
)
Co-authored-by: Martijn Pieters <mj@zopatista.com>
This commit is contained in:
parent
b01caf1d9d
commit
192711856b
3 changed files with 21 additions and 8 deletions
|
@ -243,12 +243,11 @@ class _SSLProtocolTransport(transports._FlowControlMixin,
|
||||||
The protocol's connection_lost() method will (eventually) be
|
The protocol's connection_lost() method will (eventually) be
|
||||||
called with None as its argument.
|
called with None as its argument.
|
||||||
"""
|
"""
|
||||||
self._closed = True
|
self._force_close(None)
|
||||||
if self._ssl_protocol is not None:
|
|
||||||
self._ssl_protocol._abort()
|
|
||||||
|
|
||||||
def _force_close(self, exc):
|
def _force_close(self, exc):
|
||||||
self._closed = True
|
self._closed = True
|
||||||
|
if self._ssl_protocol is not None:
|
||||||
self._ssl_protocol._abort(exc)
|
self._ssl_protocol._abort(exc)
|
||||||
|
|
||||||
def _test__append_write_backlog(self, data):
|
def _test__append_write_backlog(self, data):
|
||||||
|
@ -614,7 +613,7 @@ class SSLProtocol(protocols.BufferedProtocol):
|
||||||
if self._app_transport is not None:
|
if self._app_transport is not None:
|
||||||
self._app_transport._closed = True
|
self._app_transport._closed = True
|
||||||
if self._state == SSLProtocolState.DO_HANDSHAKE:
|
if self._state == SSLProtocolState.DO_HANDSHAKE:
|
||||||
self._abort()
|
self._abort(None)
|
||||||
else:
|
else:
|
||||||
self._set_state(SSLProtocolState.FLUSHING)
|
self._set_state(SSLProtocolState.FLUSHING)
|
||||||
self._shutdown_timeout_handle = self._loop.call_later(
|
self._shutdown_timeout_handle = self._loop.call_later(
|
||||||
|
@ -661,10 +660,10 @@ class SSLProtocol(protocols.BufferedProtocol):
|
||||||
else:
|
else:
|
||||||
self._loop.call_soon(self._transport.close)
|
self._loop.call_soon(self._transport.close)
|
||||||
|
|
||||||
def _abort(self):
|
def _abort(self, exc):
|
||||||
self._set_state(SSLProtocolState.UNWRAPPED)
|
self._set_state(SSLProtocolState.UNWRAPPED)
|
||||||
if self._transport is not None:
|
if self._transport is not None:
|
||||||
self._transport.abort()
|
self._transport._force_close(exc)
|
||||||
|
|
||||||
# Outgoing flow
|
# Outgoing flow
|
||||||
|
|
||||||
|
|
|
@ -47,6 +47,7 @@ class SslProtoHandshakeTests(test_utils.TestCase):
|
||||||
sslobj = mock.Mock()
|
sslobj = mock.Mock()
|
||||||
# emulate reading decompressed data
|
# emulate reading decompressed data
|
||||||
sslobj.read.side_effect = ssl.SSLWantReadError
|
sslobj.read.side_effect = ssl.SSLWantReadError
|
||||||
|
sslobj.write.side_effect = ssl.SSLWantReadError
|
||||||
if do_handshake is not None:
|
if do_handshake is not None:
|
||||||
sslobj.do_handshake = do_handshake
|
sslobj.do_handshake = do_handshake
|
||||||
ssl_proto._sslobj = sslobj
|
ssl_proto._sslobj = sslobj
|
||||||
|
@ -120,7 +121,19 @@ class SslProtoHandshakeTests(test_utils.TestCase):
|
||||||
test_utils.run_briefly(self.loop)
|
test_utils.run_briefly(self.loop)
|
||||||
|
|
||||||
ssl_proto._app_transport.close()
|
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):
|
def test_get_extra_info_on_closed_connection(self):
|
||||||
waiter = self.loop.create_future()
|
waiter = self.loop.create_future()
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix an ``AttributeError`` during asyncio SSL protocol aborts in SSL-over-SSL scenarios.
|
Loading…
Add table
Add a link
Reference in a new issue