Skip to content

Prevent unbounded BLERadio._connection_cache growth from causing memory exhaustion (issue #146) #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions adafruit_ble/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ def connect(self, peer, *, timeout=4.0):
if not isinstance(peer, _bleio.Address):
peer = peer.address
connection = self._adapter.connect(peer, timeout=timeout)
self._clean_connection_cache()
self._connection_cache[connection] = BLEConnection(connection)
return self._connection_cache[connection]

Expand All @@ -299,6 +300,7 @@ def connected(self):
@property
def connections(self):
"""A tuple of active `BLEConnection` objects."""
self._clean_connection_cache()
connections = self._adapter.connections
wrapped_connections = [None] * len(connections)
for i, connection in enumerate(connections):
Expand Down Expand Up @@ -337,3 +339,9 @@ def address_bytes(self):
def advertising(self):
"""The advertising state"""
return self._adapter.advertising # pylint: disable=no-member

def _clean_connection_cache(self):
"""Remove cached connections that have disconnected."""
for k, connection in list(self._connection_cache.items()):
if not connection.connected:
del self._connection_cache[k]