Skip to content

gh-159986: Fix multiprocessing manager deadlock from in-process proxy finalizers - #163223

Open
jayzuccarelli wants to merge 2 commits into
python:mainfrom
jayzuccarelli:gh-159986-manager-decref-deadlock
Open

jayzuccarelli wants to merge 2 commits into
python:mainfrom
jayzuccarelli:gh-159986-manager-decref-deadlock

Conversation

@jayzuccarelli

@jayzuccarelli jayzuccarelli commented Dec 5, 2026 •

Copy link
Copy Markdown

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 runs BaseProxy._decref, which opens a new connection to that server and waits for the reply. Two ways this deadlocks:

  • GC runs on the accepter thread (it allocates a Thread per connection). The finalizer waits in answer_challenge for a connection only the accepter could accept.
  • GC runs inside Server.create while it holds self.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 check RebuildProxy already does), put the id on a queue.SimpleQueue instead of connecting. SimpleQueue.put() is documented as safe to call from finalizers. A daemon thread started in serve_forever() drains the queue and calls Server.decref directly, so it just waits for the mutex if create holds it. Proxies of servers in other processes take the old path.

Alternatives considered:

  • Calling Server.decref inline from the finalizer fixes the accepter case but still deadlocks on the mutex inside create.
  • Making mutex an RLock would let a decref mutate the tables in the middle of create.
  • A thread per decref means starting a thread from a finalizer, which allocates and can re-enter the GC.

Notes:

  • The drain thread has the same lifecycle as the accepter: daemon, never joined. No stop protocol, since the accepter keeps serving after stop_event too.
  • In-process decrefs are now asynchronous.
  • _manager_server holds one server per process, so proxies of a second in-process server keep the old path (same limit as RebuildProxy).

Tests: new TestManagerLocalDecref.test_gc_in_server_threads (spawn suite only) runs a subprocess with GC disabled, forces gc.collect() inside create and on the accepter thread, and waits until the server's refcounts are empty, with a faulthandler.dump_traceback_later guard.

  • main without the fix: hangs, guard fires after 30 s (each case checked on its own).
  • with the fix: passes in about 2 s.
  • 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.

… 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>
@python-cla-bot

python-cla-bot Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-05T23:17:31.881068Z 8620694 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment thread Lib/multiprocessing/managers.py Outdated
Comment on lines 177 to 178
decrefer.start()
process.current_process()._manager_server = self

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant