Skip to content

gh-76067: Fix Pool hang when a task raises SystemExit - #160029

Open
dxdc wants to merge 1 commit into
python:mainfrom
dxdc:gh-76067-pool-task-systemexit
Open

dxdc wants to merge 1 commit into
python:mainfrom
dxdc:gh-76067-pool-task-systemexit

Conversation

@dxdc

@dxdc dxdc commented Oct 21, 2026 •

Copy link
Copy Markdown

A task raising a BaseException that isn't an Exception, such as SystemExit from exit() or a SIGTERM handler, ended the pool worker before the job's result was sent, so map() / AsyncResult.get() waited forever. See the analysis in the issue.

The worker now catches BaseException and sends it back like any other exception. In a worker process it is then re-raised after the result is sent, so the worker exits with the same exit code and is replaced, as before. Keeping the worker alive instead (as ProcessPoolExecutor does) would make Pool.terminate() hang on a busy worker whose SIGTERM handler raises SystemExit. ThreadPool threads report the exception and keep running, since an exited thread is only replaced when the worker handler happens to wake up.

Behavior changes callers will see:

  • get(), map(), imap() etc. now raise the task's SystemExit / KeyboardInterrupt / other BaseException instead of hanging, like Future.result() in concurrent.futures. Left uncaught in the main thread, it exits the parent with that code.
  • error_callback is called with it.
  • A ThreadPool task that calls sys.exit() no longer ends its thread.

Known limitation: for a pool created through a Manager, the manager's server only catches Exception around proxied calls (Server.serve_client), so get() on such a job now ends that client's connection (EOFError) instead of timing out. Fixing that means changing managers.py, so I've left it out to keep this PR focused, and I'm happy to follow up separately. Workers that die without raising (os._exit(), a segfault, SIGKILL) aren't covered either; that's gh-66587.

Tests cover apply_async() and the issue's map() reproducer for process and thread pools, and check that a worker process (including one started by the worker handler thread) still exits with the task's exit code and is replaced. On the current code they fail with a timeout rather than hanging. test_multiprocessing_fork, _spawn, _forkserver, _main_handling and test_concurrent_futures pass locally on Linux with --fail-env-changed, and the new tests are clean under -R 3:3. I also ran a set of scenarios against main and this branch under all three start methods (every Pool API, KeyboardInterrupt / GeneratorExit / custom BaseException, unpicklable exit codes, maxtasksperchild, buffersize, terminate() with a SIGTERM handler, real Ctrl-C, pools used from a non-main thread) and found no new hangs or crashes.

A task raising a BaseException which is not an Exception, such as
SystemExit from exit() or a SIGTERM handler, ended the worker before the
result of the job was sent, so that the pool hung forever.  The exception
is now sent back and raised by AsyncResult.get(), like other exceptions.
A worker process then exits as before and is replaced.  A ThreadPool
thread keeps running instead, since an exited thread is only replaced
when the worker handler happens to wake up.
@dxdc
dxdc requested a review from gpshead as a code owner October 21, 2026 07:10
@python-cla-bot

python-cla-bot Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

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