Repository navigation
gh-159986: Fix multiprocessing manager deadlock from in-process proxy finalizers - #163223
jayzuccarelli wants to merge 2 commits into
Conversation
… proxy finalizers A proxy finalizer sent its decref over a new connection to the manager server. When the server runs in the same process, the finalizer can run on the server's accepter thread, or while Server.create() holds the server mutex, and the server then deadlocks waiting on itself. Hand such decrefs to a SimpleQueue drained by a daemon thread that the server starts in serve_forever(). Proxies of remote servers are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8620694220
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| decrefer.start() | ||
| process.current_process()._manager_server = self |
There was a problem hiding this comment.
Register the server before starting the worker
If a same-address proxy cycle is still pending when serve_forever() starts, allocating or starting this thread can trigger cyclic GC on the serving thread. _manager_server is not set yet, so the finalizer opens a client connection instead of queuing the decref; the listener has no accepter until this call returns, and the authentication handshake blocks startup. Set _manager_server before starting this worker.
Useful? React with 👍 / 👎.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fixes a deadlock when a manager proxy is garbage collected in the same process as its server: the cleanup used to open a connection back to that server and wait on a thread that was itself blocked. It now hands the cleanup to a helper thread through a queue.
When a manager server runs in the same process as its proxies (for example
BaseManager.get_server().serve_forever()on a thread, as in apache/beam#40673), a proxy freed by the cyclic GC runsBaseProxy._decref, which opens a new connection to that server and waits for the reply. Two ways this deadlocks:Threadper connection). The finalizer waits inanswer_challengefor a connection only the accepter could accept.Server.createwhile it holdsself.mutex. The decref request is served by a new thread that blocks on that same lock.Fix: in
_decref, if this process's own server owns the proxy's address (the same checkRebuildProxyalready does), put the id on aqueue.SimpleQueueinstead of connecting.SimpleQueue.put()is documented as safe to call from finalizers. A daemon thread started inserve_forever()drains the queue and callsServer.decrefdirectly, so it just waits for the mutex ifcreateholds it. Proxies of servers in other processes take the old path.Alternatives considered:
Server.decrefinline from the finalizer fixes the accepter case but still deadlocks on the mutex insidecreate.mutexanRLockwould let a decref mutate the tables in the middle ofcreate.Notes:
stop_eventtoo._manager_serverholds one server per process, so proxies of a second in-process server keep the old path (same limit asRebuildProxy).Tests: new
TestManagerLocalDecref.test_gc_in_server_threads(spawn suite only) runs a subprocess with GC disabled, forcesgc.collect()insidecreateand on the accepter thread, and waits until the server's refcounts are empty, with afaulthandler.dump_traceback_laterguard.test_multiprocessing_spawn.test_manager+test_misc: SUCCESS (183 run, 33 skipped);test_multiprocessing_forkserver.test_manager+test_multiprocessing_fork.test_manager: SUCCESS (182 run, 38 skipped). Pydebug build of this branch, Linux.Disclosure: I used Claude Code to write the fix and test from my analysis on the issue, and reviewed it.