Skip to content

Commit a79f8fa

Browse files
pitrouvstinner
authored andcommitted
bpo-30775: Fix refleaks in test_multiprocessing (#2467)
Forgetting to call Process.join() can keep some resources alive.
1 parent ccdc09e commit a79f8fa

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

Lib/test/_test_multiprocessing.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,10 +1253,19 @@ def __init__(self, namespace, f, args, n, wait_before_exit=False):
12531253
self._can_exit = namespace.Event()
12541254
if not wait_before_exit:
12551255
self._can_exit.set()
1256+
1257+
threads = []
12561258
for i in range(n):
12571259
p = namespace.Process(target=self.task)
12581260
p.daemon = True
12591261
p.start()
1262+
threads.append(p)
1263+
1264+
def finalize(threads):
1265+
for p in threads:
1266+
p.join()
1267+
1268+
self._finalizer = weakref.finalize(self, finalize, threads)
12601269

12611270
def task(self):
12621271
pid = os.getpid()
@@ -1279,6 +1288,9 @@ def wait_for_finished(self):
12791288
def do_finish(self):
12801289
self._can_exit.set()
12811290

1291+
def close(self):
1292+
self._finalizer()
1293+
12821294

12831295
class AppendTrue(object):
12841296
def __init__(self, obj):
@@ -1311,8 +1323,11 @@ def DummyList(self):
13111323

13121324
def run_threads(self, f, args):
13131325
b = Bunch(self, f, args, self.N-1)
1314-
f(*args)
1315-
b.wait_for_finished()
1326+
try:
1327+
f(*args)
1328+
b.wait_for_finished()
1329+
finally:
1330+
b.close()
13161331

13171332
@classmethod
13181333
def multipass(cls, barrier, results, n):

0 commit comments

Comments
 (0)