Skip to content

gh-131598: Fix ProcessPoolExecutor.shutdown from a done callback - #162970

Open
jnrahme wants to merge 2 commits into
python:mainfrom
jnrahme:gh-131598-shutdown-self-join
Open

jnrahme wants to merge 2 commits into
python:mainfrom
jnrahme:gh-131598-shutdown-self-join

Conversation

@jnrahme

@jnrahme jnrahme commented Dec 1, 2026 •

Copy link
Copy Markdown

ProcessPoolExecutor runs Future done callbacks on the executor manager thread. Calling shutdown(wait=True) from such a callback tried to join() that same thread and raised RuntimeError("cannot join current thread").

YvesDup wondered in the issue whether gh-136655 / #140021 already fixed this. They did not: on current main the repro still fails at the self._executor_manager_thread.join() in ProcessPoolExecutor.shutdown.

This change:

  • skips the join when shutdown(wait=True) is invoked from the manager thread itself
  • returns without closing or clearing the manager's queues, so any work still pending can finish and the manager can clean up after the callback returns

A try/except around join() alone is not enough. After join() the method closes _result_queue and drops the queue/process references. Doing that while the manager thread is still inside the callback breaks other pending futures (OSError: handle is closed).

Regression tests cover all three start methods (fork, forkserver, spawn):

  • shutdown from a done callback no longer raises, and the future completes
  • a second pending future still completes after that shutdown

Fixes #131598

I reproduced the RuntimeError on current main, added the failing tests first, then applied the fix. ./python -m test test_concurrent_futures.test_shutdown test_concurrent_futures.test_deadlock test_concurrent_futures.test_process_pool — 248 tests, 17 skipped, OK.

AI assistance: I used an AI coding agent to reproduce the bug, draft the fix and tests, and run the test suite. I reviewed the shutdown path against the manager-thread loop so queues are not torn down while the manager still needs them.

ProcessPoolExecutor runs Future done callbacks on the executor manager
thread. Calling shutdown(wait=True) from such a callback tried to join
that same thread and raised RuntimeError("cannot join current thread").
Skip the join and leave the manager's queues intact so it can finish
any remaining work and clean up after the callback returns.

Fixes python#131598

Assisted-by: Grok Bot
ProcessPoolExecutor.shutdown is not a documented Sphinx target;
point at Executor.shutdown and the ProcessPoolExecutor class instead.
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.

concurrent.futures.ProcessPoolExecutor raises during shutdown

1 participant