Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-30775: Clear potential ref cycle between Process and Process target #2470

Merged
merged 2 commits into from
Jun 28, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 changes: 3 additions & 0 deletions Lib/multiprocessing/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dislike magic attributes which disappear. Would it be possible to assign them to None instead? Please add a reference to bpo-30775 in the comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No preference from me, but the deletion is what threading does.

_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 @@ -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)


#
#
#
Expand Down