Skip to content

gh-106558: Fix multiprocessing manager exception ref cycle. #106559

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

Merged
merged 9 commits into from
Aug 11, 2023
10 changes: 8 additions & 2 deletions Lib/multiprocessing/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ def dispatch(c, id, methodname, args=(), kwds={}):
kind, result = c.recv()
if kind == '#RETURN':
return result
raise convert_to_error(kind, result)
try:
raise convert_to_error(kind, result)
finally:
del result # break reference cycle

def convert_to_error(kind, result):
if kind == '#ERROR':
Expand Down Expand Up @@ -833,7 +836,10 @@ def _callmethod(self, methodname, args=(), kwds={}):
conn = self._Client(token.address, authkey=self._authkey)
dispatch(conn, None, 'decref', (token.id,))
return proxy
raise convert_to_error(kind, result)
try:
raise convert_to_error(kind, result)
finally:
del result # break reference cycle

def _getvalue(self):
'''
Expand Down
38 changes: 38 additions & 0 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3149,6 +3149,44 @@ def test_rapid_restart(self):
if hasattr(manager, "shutdown"):
self.addCleanup(manager.shutdown)


class FakeConnection:
def send(self, payload):
pass

def recv(self):
return '#ERROR', pyqueue.Empty()

class TestManagerExceptions(unittest.TestCase):
# Issue 106558: Manager exceptions avoids creating cyclic references.
def setUp(self):
self.mgr = multiprocessing.Manager()

def tearDown(self):
self.mgr.shutdown()
self.mgr.join()

def test_queue_get(self):
queue = self.mgr.Queue()
if gc.isenabled():
gc.disable()
self.addCleanup(gc.enable)
try:
queue.get_nowait()
except pyqueue.Empty as e:
wr = weakref.ref(e)
self.assertEqual(wr(), None)

def test_dispatch(self):
if gc.isenabled():
gc.disable()
self.addCleanup(gc.enable)
try:
multiprocessing.managers.dispatch(FakeConnection(), None, None)
except pyqueue.Empty as e:
wr = weakref.ref(e)
self.assertEqual(wr(), None)

#
#
#
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Remove ref cycle in callers of
:func:`~multiprocessing.managers.convert_to_error` by deleting ``result``
from scope in a ``finally`` block.