1
0
mirror of https://github.com/ARM-software/devlib.git synced 2025-01-31 02:00:45 +00:00

connection: Ensure we don't leak too many BackgroundCommand

Make BackgroundCommand.__init__() poll all current BackgroundCommands on
the associated connection so they deregister themselves if they are
completed.

This ensures that a BackgroundCommand-heavy application that also does
not close them properly will not accumulate useless instances forever
and leak associated resources like Popen objects.
This commit is contained in:
Douglas Raillard 2023-04-06 21:39:53 +01:00 committed by Marc Bonnici
parent 1239fd922e
commit 45aebdaca9

View File

@ -123,6 +123,19 @@ class BackgroundCommand(ABC):
def __init__(self, conn):
self.conn = conn
# Poll currently opened background commands on that connection to make
# them deregister themselves if they are completed. This avoids
# accumulating terminated commands and therefore leaking associated
# resources if the user is not careful and does not use the context
# manager API.
for bg_cmd in set(conn._current_bg_cmds):
try:
bg_cmd.poll()
# We don't want anything to fail here because of another command
except Exception:
pass
conn._current_bg_cmds.add(self)
def _deregister(self):