Skip to content

Commit 45aebda

Browse files
douglas-raillard-armmarcbonnici
authored andcommitted
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.
1 parent 1239fd9 commit 45aebda

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

devlib/connection.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,19 @@ class BackgroundCommand(ABC):
123123

124124
def __init__(self, conn):
125125
self.conn = conn
126+
127+
# Poll currently opened background commands on that connection to make
128+
# them deregister themselves if they are completed. This avoids
129+
# accumulating terminated commands and therefore leaking associated
130+
# resources if the user is not careful and does not use the context
131+
# manager API.
132+
for bg_cmd in set(conn._current_bg_cmds):
133+
try:
134+
bg_cmd.poll()
135+
# We don't want anything to fail here because of another command
136+
except Exception:
137+
pass
138+
126139
conn._current_bg_cmds.add(self)
127140

128141
def _deregister(self):

0 commit comments

Comments
 (0)