]> granicus.if.org Git - python/commitdiff
[3.5] Clear potential ref cycle between Process and Process target (GH-2470) (#2472)
authorAntoine Pitrou <pitrou@free.fr>
Wed, 28 Jun 2017 11:15:58 +0000 (13:15 +0200)
committerGitHub <noreply@github.com>
Wed, 28 Jun 2017 11:15:58 +0000 (13:15 +0200)
* 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)

Lib/multiprocessing/process.py
Lib/test/_test_multiprocessing.py

index bca8b7a0047070f054f3bd6ecfb256e709e20e5c..f9c22703df29cb2df415877c4d6ec6f58e2ab42c 100644 (file)
@@ -104,6 +104,9 @@ class BaseProcess(object):
         _cleanup()
         self._popen = self._Popen(self)
         self._sentinel = self._popen.sentinel
+        # 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
         _children.add(self)
 
     def terminate(self):
index 76209f134330a14e0a7a1c5d2ac9d20fb5dc6152..b9cd86e984e701d4ef9aea1766e7a83470ce1f76 100644 (file)
@@ -191,6 +191,12 @@ def get_value(self):
 # Testcases
 #
 
+class DummyCallable:
+    def __call__(self, q, c):
+        assert isinstance(c, DummyCallable)
+        q.put(5)
+
+
 class _TestProcess(BaseTestCase):
 
     ALLOWED_TYPES = ('processes', 'threads')
@@ -398,6 +404,18 @@ class _TestProcess(BaseTestCase):
         p.join()
         self.assertTrue(wait_for_handle(sentinel, timeout=1))
 
+    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)
+
+
 #
 #
 #