Skip to content

Fail instead of hanging when a mutant generation worker dies - #645

Open
jjohnnystwsss wants to merge 1 commit into
boxed:mainfrom
jjohnnystwsss:fix/642-dead-worker-hang
Open

jjohnnystwsss wants to merge 1 commit into
boxed:mainfrom
jjohnnystwsss:fix/642-dead-worker-hang

Conversation

@jjohnnystwsss

Copy link
Copy Markdown

Fixes #642

What changed and why

create_mutants used multiprocessing.Pool.imap_unordered. When a worker dies (in #642 the kernel's OOM killer did it), Pool starts a replacement, but the task the dead worker was running is lost. imap_unordered then waits forever for that result, so mutmut run sits at "Generating mutants" with no error.

This PR:

  • Switches create_mutants to concurrent.futures.ProcessPoolExecutor, with one future per source file. When a worker dies, the executor fails the pending futures with BrokenProcessPool instead of waiting.

  • Turns BrokenProcessPool into a new MutantGenerationWorkerDiedException. Its message names the file(s) being processed, says the usual cause is running out of memory, and suggests mutmut run --max-children 1 (the default is the number of CPUs) or more memory. Example:

    MutantGenerationWorkerDiedException: A worker process died unexpectedly while generating mutants for src/big_module.py. This usually means it was killed by the operating system for using too much memory. Try running with fewer worker processes, e.g. `mutmut run --max-children 1` (the default is the number of CPUs), or give mutmut more memory.
    
  • To know which file was being processed: each worker sets a flag for its file in a shared-memory RawArray and 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, and shutdown() 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

  • A normal per-file error, such as InvalidGeneratedSyntaxException, now surfaces later. Pool.__exit__ terminated the workers at once. ProcessPoolExecutor has 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 most max_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 in mutants/. 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_unordered read the generator lazily from a background thread. That walk is cheap.
  • When there are no source files, no worker processes are started. Pool forked them anyway.

How it was tested

  • New test test_mutant_generation_fails_when_a_worker_dies in tests/test_generation_error_handling.py. It replaces create_file_mutants with a module-level function that SIGKILLs its own worker process for one file, the same signal the OOM killer sends. The test then asserts that MutantGenerationWorkerDiedException is raised, that the message names that file, and that it mentions --max-children. A SIGALRM guard fails the test after 30 s rather than letting it hang.
  • I checked that the test fails on the old code: with the unchanged Pool code, 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.py passed 25 times in a row on Python 3.10 and 25 times on 3.14 (macOS arm64).
  • The full uv run pytest passed 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 was tests/utils/test_safe_setproctitle.py::test_setproctitle_crashes_after_fork_with_corefoundation_loaded, which is macOS-only. On unmodified main it 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.
  • Manual checks with scripts that replace 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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"Generating mutants" hangs forever when a worker is killed (e.g. out of memory), and the default worker count ignores the memory limit

1 participant