Skip to content
Closed
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
29 changes: 26 additions & 3 deletions Lib/multiprocessing/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import array
import collections.abc
import queue
import weakref
import time
import types
import os
Expand Down Expand Up @@ -144,6 +145,8 @@ class Server(object):
'''
Server class which runs in a process controlled by a manager object
'''
_local_servers = weakref.WeakValueDictionary()

public = ['shutdown', 'create', 'accept_connection', 'get_methods',
'debug_info', 'number_of_objects', 'dummy', 'incref', 'decref']

Expand All @@ -164,23 +167,37 @@ def __init__(self, registry, address, authkey, serializer):
self.id_to_refcount = {}
self.id_to_local_proxy_obj = {}
self.mutex = threading.Lock()
# Decrements from in-process proxy finalizers cannot block a thread
# that this server needs to accept incoming requests.
self._local_decrefs = queue.SimpleQueue()
self._server_pid = os.getpid()

def serve_forever(self):
'''
Run the server forever
'''
self.stop_event = threading.Event()
process.current_process()._manager_server = self
self._local_servers[self.address] = self
try:
accepter = threading.Thread(target=self.accepter)
accepter.daemon = True
accepter.start()
try:
while not self.stop_event.is_set():
self.stop_event.wait(1)
try:
ident = self._local_decrefs.get(timeout=1)
except queue.Empty:
continue
try:
self.decref(None, ident)
except Exception as e:
util.debug('... local decref failed %s', e)
except (KeyboardInterrupt, SystemExit):
pass
finally:
if self._local_servers.get(self.address) is self:
self._local_servers.pop(self.address, None)
if sys.stdout != sys.__stdout__: # what about stderr?
util.debug('resetting stdout, stderr')
sys.stdout = sys.__stdout__
Expand Down Expand Up @@ -884,8 +901,14 @@ def _decref(token, serial, authkey, state, tls, idset, _Client):
# tell manager this process no longer cares about referent
try:
util.debug('DECREF %r', token.id)
conn = _Client(token.address, authkey=authkey)
dispatch(conn, None, 'decref', (token.id,))
server = Server._local_servers.get(token.address)
if (server is not None and server._server_pid == os.getpid()):
# A finalizer may run on this server's accepter thread.
# Do not wait for that same thread to serve an RPC.
server._local_decrefs.put(token.id)
else:
conn = _Client(token.address, authkey=authkey)
dispatch(conn, None, 'decref', (token.id,))
except Exception as e:
util.debug('... decref failed %s', e)

Expand Down
77 changes: 77 additions & 0 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3751,6 +3751,83 @@ class _TestMyManager(BaseTestCase):

ALLOWED_TYPES = ('manager',)

def test_proxy_finalizer_does_not_block_local_server(self):
# gh-159986: the cyclic GC may run a proxy finalizer on the
# manager's own accepter thread, which must stay free to accept RPCs.
code = r'''
import gc
import os
import sys
import threading
import time
from multiprocessing import process
from multiprocessing.managers import BaseManager

class Manager(BaseManager):
pass

class Value:
def get(self):
return 123

Manager.register('Value', Value)
server = Manager(address=('127.0.0.1', 0), authkey=b'secret').get_server()
threading.Thread(target=server.serve_forever, daemon=True).start()
if sys.argv[1] == 'multiple':
deadline = time.monotonic() + 3
while getattr(process.current_process(), '_manager_server', None) is not server:
assert time.monotonic() < deadline
time.sleep(.001)
second_server = Manager(address=('127.0.0.1', 0), authkey=b'secret').get_server()
threading.Thread(target=second_server.serve_forever, daemon=True).start()
deadline = time.monotonic() + 3
while getattr(process.current_process(), '_manager_server', None) is not second_server:
assert time.monotonic() < deadline
time.sleep(.001)
manager = Manager(address=server.address, authkey=b'secret')
manager.connect()

# Ensure only the accepter's explicit collection can run the finalizer.
gc.disable()
proxy = manager.Value()
cycle = [proxy]
cycle.append(cycle)
del proxy, cycle

triggered = threading.Event()
original_init = threading.Thread.__init__

def collect_during_accept(self, *args, **kwargs):
target = kwargs.get('target')
if (getattr(target, '__self__', None) is server and
getattr(target, '__name__', '') == 'handle_request' and
not triggered.is_set()):
triggered.set()
gc.collect()
return original_init(self, *args, **kwargs)

threading.Thread.__init__ = collect_during_accept
result = []

def request():
result.append(manager.Value().get())

worker = threading.Thread(target=request, daemon=True)
worker.start()
worker.join(timeout=10)
if worker.is_alive():
os._exit(2)
assert triggered.is_set() and result == [123], result

# The local decrement is asynchronous, but it must actually be processed.
deadline = time.monotonic() + 5
while server.id_to_refcount and time.monotonic() < deadline:
time.sleep(0.01)
assert not server.id_to_refcount, server.id_to_refcount
'''
for variant in ('single', 'multiple'):
test.support.script_helper.assert_python_ok('-c', code, variant)

@warnings_helper.ignore_fork_in_thread_deprecation_warnings()
@support.skip_if_sanitizer('TSan: leaks threads', thread=True)
def test_mymanager(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a deadlock in :mod:`multiprocessing.managers` when cyclic garbage
collection finalizes a proxy on its own manager server's accepting thread.
Loading