From e69b02a21d8c0f9de1dc01731c3de120e318d3e0 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Wed, 28 Jun 2017 11:50:11 +0200 Subject: [PATCH 1/2] 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. --- Lib/multiprocessing/process.py | 3 +++ Lib/test/_test_multiprocessing.py | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/Lib/multiprocessing/process.py b/Lib/multiprocessing/process.py index 70bb50d99911ca..bd680d7174eed2 100644 --- a/Lib/multiprocessing/process.py +++ b/Lib/multiprocessing/process.py @@ -110,6 +110,9 @@ def start(self): _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 + 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 7dae3c498d2f21..d0a5446cfea2c6 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') @@ -469,6 +475,18 @@ def test_many_processes(self): 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) + + # # # From 7b0b53249b698c9bd1019826e219ed8db02fa9ce Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Wed, 28 Jun 2017 12:07:35 +0200 Subject: [PATCH 2/2] Add issue reference --- Lib/multiprocessing/process.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/multiprocessing/process.py b/Lib/multiprocessing/process.py index bd680d7174eed2..fde97b7a67e4c8 100644 --- a/Lib/multiprocessing/process.py +++ b/Lib/multiprocessing/process.py @@ -111,7 +111,7 @@ def start(self): 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 + # reference to the process object (see bpo-30775) del self._target, self._args, self._kwargs _children.add(self)