From de48835082f8e1588d036c8884bc782c9d7c15fb Mon Sep 17 00:00:00 2001 From: ordinary-jamie <101677823+ordinary-jamie@users.noreply.github.com> Date: Thu, 21 Dec 2023 09:58:36 +1100 Subject: [PATCH 1/2] Release asyncio SSLProtocol buffer on error When the `asyncio.sslproto.SSLProtocol` raises on handshake, it doesn't appear to release its SSL buffer, causing a memory leak. This is because of the `memoryview`. This commit releases the `memoryview` before unsetting the reference to the `bytearray` --- Lib/asyncio/sslproto.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Lib/asyncio/sslproto.py b/Lib/asyncio/sslproto.py index 3eb65a8a08b5a0..f668b0d1be60f9 100644 --- a/Lib/asyncio/sslproto.py +++ b/Lib/asyncio/sslproto.py @@ -910,6 +910,9 @@ def _fatal_error(self, exc, message='Fatal error on transport'): if self._transport: self._transport._force_close(exc) + self._ssl_buffer_view.release() + self._ssl_buffer = None + if isinstance(exc, OSError): if self._loop.get_debug(): logger.debug("%r: %s", self, message, exc_info=True) From d103582267d2e6b50f257f7f297e77a8366d5ac9 Mon Sep 17 00:00:00 2001 From: ordinary-jamie <101677823+ordinary-jamie@users.noreply.github.com> Date: Thu, 21 Dec 2023 10:02:32 +1100 Subject: [PATCH 2/2] add news --- .../next/Library/2023-12-21-10-02-18.gh-issue-109534.VujEyM.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2023-12-21-10-02-18.gh-issue-109534.VujEyM.rst diff --git a/Misc/NEWS.d/next/Library/2023-12-21-10-02-18.gh-issue-109534.VujEyM.rst b/Misc/NEWS.d/next/Library/2023-12-21-10-02-18.gh-issue-109534.VujEyM.rst new file mode 100644 index 00000000000000..695816c1a3325d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-12-21-10-02-18.gh-issue-109534.VujEyM.rst @@ -0,0 +1,2 @@ +Fix a memory leak in :class:`asyncio.sslproto.SSLProtocol` when the SSL +handshake fails. Contributed by Jamie Phan.