Skip to content

fix(parse): pool and timeout workers exit when their parent dies - #487

Merged
HananKavitzAmat merged 1 commit into
chunkhound:mainfrom
fstrasz:fix/parse-pool-parent-death
Nov 8, 2026
Merged

HananKavitzAmat merged 1 commit into
chunkhound:mainfrom
fstrasz:fix/parse-pool-parent-death

Conversation

@fstrasz

@fstrasz fstrasz commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Problem

A ProcessPoolExecutor worker waits on its call queue, and the queue is pickled into the worker with both pipe ends, so the worker never sees the pipe close when its parent dies. When a ChunkHound process exits abnormally (killed MCP server, crashed daemon, TerminateProcess, SIGKILL), every parse worker it started keeps running until someone kills it. On Windows each one held 35-370 MB of commit in our measurements, and they accumulate across killed sessions.

Fix

chunkhound/utils/parent_watch.py adds exit_when_parent_dies(). Every multiprocessing child already has a sentinel for its parent in multiprocessing.parent_process() (a process handle on Windows, a pipe on POSIX). A daemon thread waits on it and calls os._exit(1) when the parent is gone.

It is wired in as:

  1. the initializer of the parse pool (pipeline_bridge._get_parse_pool);
  2. the initializer of both ProcessPoolExecutors in indexing_coordinator;
  3. the first call in both per-file timeout children (pipeline_bridge._parse_file_worker_for_timeout, batch_processor._parse_file_worker), so a timeout child of a dying worker goes too.

Alternative considered: a Windows job object with JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE

Rejected because it is Windows-only while the orphaning happens on every platform, and because ProcessPoolExecutor spawns workers lazily with no hook to assign each one to a job after it starts.

Test

tests/contracts/test_parse_pool_parent_death.py starts the real parse pool in a child Python process, has it spawn a worker, hard-kills that parent, and asserts the worker exits within 20 s.

  1. Before the fix: fails ("parse-pool worker NNNN outlived its killed parent").
  2. After: passes in about 1 s.

Related tests (contracts, batch processor timeout, coordinator, root indexing, skip-unchanged, pipeline bridge config, unknown files) run serially on Windows: 162 passed, 5 skipped.

🤖 Generated with Claude Code

A ProcessPoolExecutor worker blocks on its call queue, whose pipe it holds
both ends of, so it never notices its parent dying. A killed ChunkHound
process left every parse worker running, each holding hundreds of MB of
commit, until killed by hand.

Every multiprocessing child already has a sentinel for its parent
(multiprocessing.parent_process()): a process handle on Windows, a pipe on
POSIX. exit_when_parent_dies() waits on it from a daemon thread and exits
the process when the parent is gone. It is the initializer of the parse
pool and of both indexing_coordinator pools, and the first call in both
per-file timeout children.

A Windows job object with KILL_ON_JOB_CLOSE was the alternative: it is
Windows-only (the same orphaning happens on POSIX), and each worker would
have to be assigned to the job after ProcessPoolExecutor spawns it, which
the executor gives no hook for.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@HananKavitzAmat

Copy link
Copy Markdown
Collaborator

Looks good! Thanks

@HananKavitzAmat
HananKavitzAmat added this pull request to the merge queue Nov 8, 2026
Merged via the queue into chunkhound:main with commit 4a03648 Nov 8, 2026
25 checks passed
fstrasz added a commit to fstrasz/chunkhound that referenced this pull request Nov 11, 2026
One conflict, in chunkhound/pipeline_bridge.py _get_parse_pool: chunkhound#487 on main
added initializer=exit_when_parent_dies to the same ProcessPoolExecutor call
this branch gave mp_context=multiprocessing.get_context("spawn"). Kept both
arguments and this branch's comment. exit_when_parent_dies is a module-level
function, so it pickles under spawn.

Both arguments are load-bearing: without mp_context,
test_parse_pool_is_created_with_a_spawn_context fails; without the
initializer, chunkhound#487's test_parse_pool_worker_exits_when_parent_is_killed fails.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

2 participants