summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAntoine Pitrou <pitrou@free.fr>2017-06-28 11:15:58 (GMT)
committerGitHub <noreply@github.com>2017-06-28 11:15:58 (GMT)
commita9705b777859f555d50eb5dcd5fc4260c42a0188 (patch)
treefab574b858e11c89f7ee7a34beb422f59457148f
parentf15bf1f3f3104f6ab2229e4b359984489a74685b (diff)
downloadcpython-a9705b777859f555d50eb5dcd5fc4260c42a0188.zip
cpython-a9705b777859f555d50eb5dcd5fc4260c42a0188.tar.gz
cpython-a9705b777859f555d50eb5dcd5fc4260c42a0188.tar.bz2
[3.5] Clear potential ref cycle between Process and Process target (GH-2470) (#2472)
* 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)
-rw-r--r--Lib/multiprocessing/process.py3
-rw-r--r--Lib/test/_test_multiprocessing.py18
2 files changed, 21 insertions, 0 deletions
diff --git a/Lib/multiprocessing/process.py b/Lib/multiprocessing/process.py
index bca8b7a..f9c2270 100644
--- a/Lib/multiprocessing/process.py
+++ b/Lib/multiprocessing/process.py
@@ -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):
diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py
index 76209f1..b9cd86e 100644
--- a/Lib/test/_test_multiprocessing.py
+++ b/Lib/test/_test_multiprocessing.py
@@ -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)
+
+
#
#
#