Skip to content

Commit

Permalink
Disable cleanup_closed for aiohttp.TCPConnector with cpython 3.11.1+ (#…
Browse files Browse the repository at this point in the history
…93013)

* 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 aio-libs/aiohttp#7252
see python/cpython#98540

* Update homeassistant/helpers/aiohttp_client.py
  • Loading branch information
bdraco authored and balloob committed May 14, 2023
1 parent 304c34a commit 13c51e9
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion homeassistant/helpers/aiohttp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
APPLICATION_NAME, __version__, aiohttp.__version__, sys.version_info
)

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

WARN_CLOSE_MSG = "closes the Home Assistant aiohttp session"

#
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 13c51e9

Please sign in to comment.