Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[3.5] Clear potential ref cycle between Process and Process target (G…
…H-2470)

* 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 79d37ae)
  • Loading branch information
pitrou committed Jun 28, 2017
commit 5f2ab582312f0e26848aaa2ba2c1c318a2d78b6f
3 changes: 3 additions & 0 deletions Lib/multiprocessing/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,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 (see bpo-30775)
del self._target, self._args, self._kwargs
_children.add(self)

def terminate(self):
Expand Down
18 changes: 18 additions & 0 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -398,6 +404,18 @@ def test_sentinel(self):
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)


#
#
#
Expand Down