From 2cf8aded691807c0e74d8c4b756af9500d65a32a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 13 May 2023 08:59:16 +0900 Subject: [PATCH 1/2] Disable cleanup_closed for aiohttp.TCPConnector with cpython 3.11.2+ There is currently a relatively fast memory leak when using cpython 3.11.2+ and cleanup_closed with aiohttp For my production instance it was leaking ~450MiB per day of `MemoryBIO`, `SSLProtocol`, `SSLObject`, `_SSLProtocolTransport` `memoryview`, and `managedbuffer` objects see https://github.com/aio-libs/aiohttp/issues/7252 see https://github.com/python/cpython/pull/98540 --- homeassistant/helpers/aiohttp_client.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/homeassistant/helpers/aiohttp_client.py b/homeassistant/helpers/aiohttp_client.py index 78a8051df1cc7d..d0e23a8bb7b7e9 100644 --- a/homeassistant/helpers/aiohttp_client.py +++ b/homeassistant/helpers/aiohttp_client.py @@ -37,6 +37,11 @@ APPLICATION_NAME, __version__, aiohttp.__version__, sys.version_info ) +ENABLE_CLEANUP_CLOSED = sys.version_info < (3, 11, 2) +# Enabling cleanup closed on python 3.11.2+ leaks memory relatively quickly +# see https://github.com/aio-libs/aiohttp/issues/7252 +# aiohttp interacts poorly with https://github.com/python/cpython/pull/98540 + WARN_CLOSE_MSG = "closes the Home Assistant aiohttp session" # @@ -276,7 +281,7 @@ def _async_get_connector( ssl_context = ssl_util.get_default_no_verify_context() connector = aiohttp.TCPConnector( - enable_cleanup_closed=True, + enable_cleanup_closed=ENABLE_CLEANUP_CLOSED, ssl=ssl_context, limit=MAXIMUM_CONNECTIONS, limit_per_host=MAXIMUM_CONNECTIONS_PER_HOST, From bfb0d164b90386ceaeb3c05cc48674f717e5a58c Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 13 May 2023 09:22:44 +0900 Subject: [PATCH 2/2] Update homeassistant/helpers/aiohttp_client.py --- homeassistant/helpers/aiohttp_client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/helpers/aiohttp_client.py b/homeassistant/helpers/aiohttp_client.py index d0e23a8bb7b7e9..78806cb5ae174c 100644 --- a/homeassistant/helpers/aiohttp_client.py +++ b/homeassistant/helpers/aiohttp_client.py @@ -37,8 +37,8 @@ APPLICATION_NAME, __version__, aiohttp.__version__, sys.version_info ) -ENABLE_CLEANUP_CLOSED = sys.version_info < (3, 11, 2) -# Enabling cleanup closed on python 3.11.2+ leaks memory relatively quickly +ENABLE_CLEANUP_CLOSED = sys.version_info < (3, 11, 1) +# Enabling cleanup closed on python 3.11.1+ leaks memory relatively quickly # see https://github.com/aio-libs/aiohttp/issues/7252 # aiohttp interacts poorly with https://github.com/python/cpython/pull/98540