Repository navigation
Fail instead of hanging when a mutant generation worker dies - #645
Open
jjohnnystwsss wants to merge 1 commit into
Open
jjohnnystwsss wants to merge 1 commit into
jjohnnystwsss wants to merge 1 commit into
Conversation
create_mutants used multiprocessing.Pool.imap_unordered. When a worker dies, for example killed by the kernel for using too much memory, Pool replaces it but the task it was running is lost, so "Generating mutants" waited forever for its result. Use concurrent.futures.ProcessPoolExecutor, which raises BrokenProcessPool when a worker dies, and turn that into an error that names the file(s) being processed and suggests a lower --max-children or more memory. Workers flag the file they are working on in shared memory, which can still be read after a worker is killed. Workers die on Ctrl-C like Pool workers did, so an interrupt doesn't wait for queued files. Fixes boxed#642 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Fixes #642
What changed and why
create_mutantsusedmultiprocessing.Pool.imap_unordered. When a worker dies (in #642 the kernel's OOM killer did it),Poolstarts a replacement, but the task the dead worker was running is lost.imap_unorderedthen waits forever for that result, somutmut runsits at "Generating mutants" with no error.This PR:
Switches
create_mutantstoconcurrent.futures.ProcessPoolExecutor, with one future per source file. When a worker dies, the executor fails the pending futures withBrokenProcessPoolinstead of waiting.Turns
BrokenProcessPoolinto a newMutantGenerationWorkerDiedException. Its message names the file(s) being processed, says the usual cause is running out of memory, and suggestsmutmut run --max-children 1(the default is the number of CPUs) or more memory. Example:To know which file was being processed: each worker sets a flag for its file in a shared-memory
RawArrayand clears it when done. Shared memory can still be read after a worker is killed. The executor doesn't say which worker died, so with several workers the message lists every file being processed at that moment ("for one of a.py, b.py"). With one worker, or one file in flight, it names the exact file.In the worker initializer, SIGINT goes back to its default action. Pool workers died on Ctrl-C. Executor workers would instead report the
KeyboardInterrupt, start on the files already queued, andshutdown()would wait for those files. I measured this: with all workers busy, Ctrl-C took about 10 s with the executor and 0.1 s after this change, the same as before.Everything else is unchanged: progress output (the
print(path)from the workers that drives the spinner), unordered result handling, how warnings are forwarded,raise result.error, and the stats. The "memory-aware default worker count" idea from the issue is not part of this PR.Behaviour differences a reviewer may want to check
InvalidGeneratedSyntaxException, now surfaces later.Pool.__exit__terminated the workers at once.ProcessPoolExecutorhas no public way to do that before Python 3.14.shutdown(cancel_futures=True)drops the files that no worker has received yet, but it waits for the files already being processed and for the few already in the executor's call queue (at mostmax_children + 1). In a synthetic test (4 workers, files taking 3 s each, one file erroring after 0.5 s) the error appeared after about 9 s instead of 0.5 s. The exception and its message are the same. A side effect is that workers are no longer killed halfway through writing a file inmutants/. If you'd prefer the old timing, a shared "stop" flag that workers check before each file would cut the wait to at most one file per worker.walk_source_files()is collected into a list up front.imap_unorderedread the generator lazily from a background thread. That walk is cheap.Poolforked them anyway.How it was tested
test_mutant_generation_fails_when_a_worker_diesintests/test_generation_error_handling.py. It replacescreate_file_mutantswith a module-level function that SIGKILLs its own worker process for one file, the same signal the OOM killer sends. The test then asserts thatMutantGenerationWorkerDiedExceptionis raised, that the message names that file, and that it mentions--max-children. ASIGALRMguard fails the test after 30 s rather than letting it hang.Poolcode, the test was stopped by the timeout (TimeoutError: still running after 10s, in a temporary copy that used a 10 s limit) instead of getting an error. Pytest exited cleanly afterwards.tests/test_generation_error_handling.pypassed 25 times in a row on Python 3.10 and 25 times on 3.14 (macOS arm64).uv run pytestpassed locally on 3.10, 3.11, 3.12, 3.13 and 3.14 (macOS arm64), on the version of this change before the SIGINT line was added. After adding that line, only the test file above, the linters and the Ctrl-C check were run again. The only failure wastests/utils/test_safe_setproctitle.py::test_setproctitle_crashes_after_fork_with_corefoundation_loaded, which is macOS-only. On unmodifiedmainit fails about half the time on this machine, and it is skipped on Linux CI. On 3.12 and later, the new test adds two more of the existing "process is multi-threaded, use of fork() may lead to deadlocks"DeprecationWarnings when it runs inside the full suite. It shows none when the file runs alone.uv run pre-commit run --all-files(ruff check, ruff format, mypy and the other hooks) passes.create_file_mutants: the error message with 1, 2 and 8 workers, Ctrl-C (SIGINT to the process group) with all workers busy, and the timing of the normal-error path described above.Not tested: a real OOM kill inside a memory-limited container, CI on Linux, and Python 3.15.
AI disclosure
Claude Code (Claude Opus 5.5) wrote this change, the test and this description. It ran the commands and measurements listed above. This text does not claim that a human reviewed the change.
🤖 Generated with Claude Code