From 249121beae8300270ab1647e449f5c170b85d118 Mon Sep 17 00:00:00 2001 From: Geraldo Nascimento Date: Mon, 19 Feb 2024 00:14:11 -0300 Subject: [PATCH] pythongh-109534: get rid of bytearray as class member Workaround horrendous memory leaks, does not solve the underlying issue of why bytearray as class member like that will cause leaks, at least in this case --- Lib/asyncio/selector_events.py | 2 +- Lib/asyncio/sslproto.py | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/Lib/asyncio/selector_events.py b/Lib/asyncio/selector_events.py index 8e888d26ea0737..85cb05a2dedcc3 100644 --- a/Lib/asyncio/selector_events.py +++ b/Lib/asyncio/selector_events.py @@ -989,7 +989,7 @@ def _read_ready__get_buffer(self): return try: - self._protocol.buffer_updated(nbytes) + self._protocol.buffer_updated(nbytes, buf[:nbytes]) except (SystemExit, KeyboardInterrupt): raise except BaseException as exc: diff --git a/Lib/asyncio/sslproto.py b/Lib/asyncio/sslproto.py index fa99d4533aa0a6..3d6944d05a8592 100644 --- a/Lib/asyncio/sslproto.py +++ b/Lib/asyncio/sslproto.py @@ -275,8 +275,8 @@ def __init__(self, loop, app_protocol, sslcontext, waiter, if ssl is None: raise RuntimeError("stdlib ssl module not available") - self._ssl_buffer = bytearray(self.max_size) - self._ssl_buffer_view = memoryview(self._ssl_buffer) + self._ssl_buffer = None + self._ssl_buffer_view = None if ssl_handshake_timeout is None: ssl_handshake_timeout = constants.SSL_HANDSHAKE_TIMEOUT @@ -427,13 +427,12 @@ def get_buffer(self, n): want = n if want <= 0 or want > self.max_size: want = self.max_size - if len(self._ssl_buffer) < want: - self._ssl_buffer = bytearray(want) - self._ssl_buffer_view = memoryview(self._ssl_buffer) - return self._ssl_buffer_view + _ssl_buffer = bytearray(want) + _ssl_buffer_view = memoryview(_ssl_buffer) + return _ssl_buffer_view - def buffer_updated(self, nbytes): - self._incoming.write(self._ssl_buffer_view[:nbytes]) + def buffer_updated(self, nbytes, buffer): + self._incoming.write(buffer) if self._state == SSLProtocolState.DO_HANDSHAKE: self._do_handshake()