]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Clear potential ref cycle between Process and Process target (#2470)
authorAntoine Pitrou <pitrou@free.fr>
Wed, 28 Jun 2017 10:29:08 +0000 (12:29 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Wed, 28 Jun 2017 10:29:08 +0000 (12:29 +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

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

index 70bb50d99911cadae16fdc09453dff50e09cf46f..fde97b7a67e4c8d8370dc341f6e1b300e01da383 100644 (file)
@@ -110,6 +110,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 7dae3c498d2f21ffe67404f62f2f0115261acb67..d0a5446cfea2c67f8bb5fc77a56b1cffa863df82 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')
@@ -469,6 +475,18 @@ class _TestProcess(BaseTestCase):
             for p in procs:
                 self.assertEqual(p.exitcode, -signal.SIGTERM)
 
+    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)
+
+
 #
 #
 #