From: Antoine Pitrou Date: Wed, 28 Jun 2017 09:49:38 +0000 (+0200) Subject: [3.6] bpo-30775: Fix refleaks in test_multiprocessing (GH-2467) (#2468) X-Git-Tag: v3.6.3rc1~286 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e022aad73a4151b5628e2476a8465ce6c0d18b8c;p=python [3.6] bpo-30775: Fix refleaks in test_multiprocessing (GH-2467) (#2468) Forgetting to call Process.join() can keep some resources alive. (cherry picked from commit a79f8faccf5e26f55e8b9496ad49d2071b5e299c) --- diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py index cd2c8dbc48..e76c457022 100644 --- a/Lib/test/_test_multiprocessing.py +++ b/Lib/test/_test_multiprocessing.py @@ -1181,10 +1181,19 @@ class Bunch(object): self._can_exit = namespace.Event() if not wait_before_exit: self._can_exit.set() + + threads = [] for i in range(n): p = namespace.Process(target=self.task) p.daemon = True p.start() + threads.append(p) + + def finalize(threads): + for p in threads: + p.join() + + self._finalizer = weakref.finalize(self, finalize, threads) def task(self): pid = os.getpid() @@ -1207,6 +1216,9 @@ class Bunch(object): def do_finish(self): self._can_exit.set() + def close(self): + self._finalizer() + class AppendTrue(object): def __init__(self, obj): @@ -1239,8 +1251,11 @@ class _TestBarrier(BaseTestCase): def run_threads(self, f, args): b = Bunch(self, f, args, self.N-1) - f(*args) - b.wait_for_finished() + try: + f(*args) + b.wait_for_finished() + finally: + b.close() @classmethod def multipass(cls, barrier, results, n):