]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-109534: fix reference leak when SSL handshake fails (GH-114074) (#114830)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 1 Feb 2024 00:59:17 +0000 (01:59 +0100)
committerGitHub <noreply@github.com>
Thu, 1 Feb 2024 00:59:17 +0000 (00:59 +0000)
gh-109534: fix reference leak when SSL handshake fails (GH-114074)
(cherry picked from commit 80aa7b3688b8fdc85cd53d4113cb5f6ce5500027)

Co-authored-by: Jamie Phan <jamie@ordinarylab.dev>
Lib/asyncio/selector_events.py
Lib/asyncio/sslproto.py
Misc/NEWS.d/next/Library/2024-01-15-18-42-44.gh-issue-109534.wYaLMZ.rst [new file with mode: 0644]

index 96e61f7c3a4e9fd1dc8c392de524bb09e2650e3e..40df1b7da5c785eadd9905afba808a8b4525258e 100644 (file)
@@ -222,6 +222,10 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
                 await waiter
             except BaseException:
                 transport.close()
+                # gh-109534: When an exception is raised by the SSLProtocol object the
+                # exception set in this future can keep the protocol object alive and
+                # cause a reference cycle.
+                waiter = None
                 raise
                 # It's now up to the protocol to handle the connection.
 
index 4d2cf8224ae4cf17446f76671bba5b48563db851..e51669a2ab2af6d882f74423c4e69a51c4b67406 100644 (file)
@@ -579,6 +579,7 @@ class SSLProtocol(protocols.BufferedProtocol):
 
             peercert = sslobj.getpeercert()
         except Exception as exc:
+            handshake_exc = None
             self._set_state(SSLProtocolState.UNWRAPPED)
             if isinstance(exc, ssl.CertificateError):
                 msg = 'SSL handshake failed on verifying the certificate'
diff --git a/Misc/NEWS.d/next/Library/2024-01-15-18-42-44.gh-issue-109534.wYaLMZ.rst b/Misc/NEWS.d/next/Library/2024-01-15-18-42-44.gh-issue-109534.wYaLMZ.rst
new file mode 100644 (file)
index 0000000..fc9a765
--- /dev/null
@@ -0,0 +1,3 @@
+Fix a reference leak in
+:class:`asyncio.selector_events.BaseSelectorEventLoop` when SSL handshakes
+fail. Patch contributed by Jamie Phan.