Skip to content

Commit f15bf1f

Browse files
pitrouvstinner
authored andcommitted
[3.5] bpo-30775: Fix refleaks in test_multiprocessing (GH-2467) (#2469)
Forgetting to call Process.join() can keep some resources alive. (cherry picked from commit a79f8fa)
1 parent 54ba940 commit f15bf1f

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

Lib/test/_test_multiprocessing.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,10 +1181,19 @@ def __init__(self, namespace, f, args, n, wait_before_exit=False):
11811181
self._can_exit = namespace.Event()
11821182
if not wait_before_exit:
11831183
self._can_exit.set()
1184+
1185+
threads = []
11841186
for i in range(n):
11851187
p = namespace.Process(target=self.task)
11861188
p.daemon = True
11871189
p.start()
1190+
threads.append(p)
1191+
1192+
def finalize(threads):
1193+
for p in threads:
1194+
p.join()
1195+
1196+
self._finalizer = weakref.finalize(self, finalize, threads)
11881197

11891198
def task(self):
11901199
pid = os.getpid()
@@ -1207,6 +1216,9 @@ def wait_for_finished(self):
12071216
def do_finish(self):
12081217
self._can_exit.set()
12091218

1219+
def close(self):
1220+
self._finalizer()
1221+
12101222

12111223
class AppendTrue(object):
12121224
def __init__(self, obj):
@@ -1239,8 +1251,11 @@ def DummyList(self):
12391251

12401252
def run_threads(self, f, args):
12411253
b = Bunch(self, f, args, self.N-1)
1242-
f(*args)
1243-
b.wait_for_finished()
1254+
try:
1255+
f(*args)
1256+
b.wait_for_finished()
1257+
finally:
1258+
b.close()
12441259

12451260
@classmethod
12461261
def multipass(cls, barrier, results, n):

0 commit comments

Comments
 (0)