]> granicus.if.org Git - python/commitdiff
[2.7] Clear potential ref cycle between Process and Process target (GH-2470) (#2473)
authorAntoine Pitrou <pitrou@free.fr>
Wed, 28 Jun 2017 11:48:38 +0000 (13:48 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Wed, 28 Jun 2017 11:48:38 +0000 (13:48 +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 f6b03b192ae7f9e66dc087c2045588fc12d40ccc..16c4e1eb3437367b4e387925ff6c62b5f0001660 100644 (file)
@@ -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):
index 9fcd3bdad4189b69396bb67a00620da8df8ff2f4..69bd8f79a85568895bf93970787f4b553e4adbc0 100644 (file)
@@ -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)
+
+
 #
 #
 #