From: Antoine Pitrou Date: Wed, 28 Jun 2017 11:48:38 +0000 (+0200) Subject: [2.7] Clear potential ref cycle between Process and Process target (GH-2470) (#2473) X-Git-Tag: v2.7.14rc1~72 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=12536bd261ba95cd2748f3d7d47768742a6ffa7a;p=python [2.7] Clear potential ref cycle between Process and Process target (GH-2470) (#2473) * Clear potential ref cycle between Process and Process target Besides Process.join() not being called, this was an indirect cause of bpo-30775. The threading module already does this. * Add issue reference. (cherry picked from commit 79d37ae979a65ada0b2ac820279ccc3b1cd41ba6) --- diff --git a/Lib/multiprocessing/process.py b/Lib/multiprocessing/process.py index f6b03b192a..16c4e1eb34 100644 --- a/Lib/multiprocessing/process.py +++ b/Lib/multiprocessing/process.py @@ -128,6 +128,9 @@ class Process(object): else: from .forking import Popen self._popen = Popen(self) + # Avoid a refcycle if the target function holds an indirect + # reference to the process object (see bpo-30775) + del self._target, self._args, self._kwargs _current_process._children.add(self) def terminate(self): diff --git a/Lib/test/test_multiprocessing.py b/Lib/test/test_multiprocessing.py index 9fcd3bdad4..69bd8f79a8 100644 --- a/Lib/test/test_multiprocessing.py +++ b/Lib/test/test_multiprocessing.py @@ -175,6 +175,12 @@ def get_value(self): # Testcases # +class DummyCallable(object): + def __call__(self, q, c): + assert isinstance(c, DummyCallable) + q.put(5) + + class _TestProcess(BaseTestCase): ALLOWED_TYPES = ('processes', 'threads') @@ -355,6 +361,18 @@ class _TestProcess(BaseTestCase): p.join(5) self.assertEqual(p.exitcode, reason) + def test_lose_target_ref(self): + c = DummyCallable() + wr = weakref.ref(c) + q = self.Queue() + p = self.Process(target=c, args=(q, c)) + del c + p.start() + p.join() + self.assertIs(wr(), None) + self.assertEqual(q.get(), 5) + + # # #