Prevent process pool zombies on crash#3207
Merged
Merged
Conversation
…lation When the parent process is killed without cleanup (OOM, SIGKILL, etc.), ProcessPoolExecutor workers become orphans that sit forever on dead IPC pipes. Setting PR_SET_PDEATHSIG(SIGTERM) in the worker initializer makes the kernel signal workers on parent death, regardless of how the parent died.
The test subprocess imported bbot which sets mp.set_start_method("spawn"),
making workers slow to initialize (full bbot import per worker). In CI
containers the 0.5s sleep wasn't enough for workers to call prctl before
the parent was killed. Now the test is self-contained with no bbot imports,
uses a temp file so spawn-mode workers can import the initializer, and
gives workers 2s to start.
Contributor
🚀 Performance Benchmark Report
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## dev #3207 +/- ##
======================================
+ Coverage 90% 90% +1%
======================================
Files 453 453
Lines 46101 46278 +177
======================================
+ Hits 41216 41396 +180
+ Misses 4885 4882 -3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Get worker PIDs via futures instead of private pool._processes dict.
Use SIGKILL instead of SIGTERM for PR_SET_PDEATHSIG since ProcessPoolExecutor's except BaseException catches SIGTERM's SystemExit. Loop PID collection to ensure unique worker PIDs. Check /proc/PID/stat instead of os.kill(pid, 0) to distinguish zombies from running processes.
Python 3.14 defaults to forkserver, which re-imports the script file. Without the guard the forkserver child re-executes pool creation and crashes.
With forkserver (Python 3.14 default), worker spawn is slow enough that fast sequential tasks all route to the first worker. Submit concurrent 1s tasks so the pool is forced to dispatch to both workers.
Forkserver adds an intermediary process that stalls pool startup in CI.
Use mp.get_context("fork") since PR_SET_PDEATHSIG is start-method-agnostic.
Also drops the __main__ guard (not needed with fork context).
ausmaster
approved these changes
Jun 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ProcessPoolExecutorworkers become orphans reparented to PID 1, sitting forever on dead IPC pipes. They accumulate across crashes.prctl(PR_SET_PDEATHSIG, SIGTERM)in each pool worker's initializer so the kernel signals workers on parent death, regardless of how the parent died. ~15 lines, stdlib only (ctypes), Linux-only (no-op elsewhere).test_pool_workers_die_with_parentwhich spawns a subprocess with a pool, SIGKILL's the parent, and asserts workers are dead within 2 seconds.closes #3203