Skip to content

fix(pipeline): parse pool always starts workers with spawn (MCP start-up scan hangs on Linux) - #493

Open
fstrasz wants to merge 4 commits into
chunkhound:mainfrom
fstrasz:fix/parse-pool-spawn
Open

fstrasz wants to merge 4 commits into
chunkhound:mainfrom
fstrasz:fix/parse-pool-spawn

Conversation

@fstrasz

@fstrasz fstrasz commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Problem

On Linux, chunkhound mcp can stay at files_processed: 0 / query_ready: false forever after start-up.

The parse pool in pipeline_bridge._get_parse_pool() is created with no mp_context, so it uses the platform default, which on Linux (Python < 3.14) is fork. An MCP server creates the pool from a process that already runs threads, among them the stdio reader that holds sys.stdin's lock. A forked worker inherits that lock in the held state and blocks in multiprocessing.util._close_stdin() before it runs any task. The parse thread then waits on the worker's future forever.

indexing_coordinator._ensure_mp_start_method() sets the global start method to spawn, but the realtime/MCP start-up path can create this pool before that function runs. chunkhound index is not affected, because it creates the pool before any such thread exists.

Evidence

Measured in a Linux container (Debian trixie, Python 3.12) with a one-file scratch project and realtime_backend: watchdog:

  1. py-spy dump of a parse worker showed _close_stdin (multiprocessing/util.py) under ProcessPoolExecutor._spawn_process / ForkProcess. The server's parse thread was in Future.result under parse_batch_callback.
  2. Unpatched: still is_scanning: true, files_processed: 0 after 120 s.
  3. Same image with the pool given mp_context=get_context("spawn"): query_ready in 13.8 s (2 files, 13 chunks).

Windows and macOS already default to spawn, which is why this only shows up on Linux.

Change

_get_parse_pool() passes mp_context=multiprocessing.get_context("spawn"), the same choice batch_processor.py and the per-file timeout worker in this module already make.

Test

tests/contracts/test_parse_pool_spawn_context.py records the mp_context the pool is constructed with and asserts its start method is spawn. It checks the context the pool is given, not the platform default, so it fails before the fix on every OS. A real fork deadlock can't be reproduced on a Windows CI runner. The existing test_parse_pool_reused.py still passes.

Broader run (Windows 11, Python 3.12, serial). This was on our fork branch, which carries the same change on top of other pending fixes. tests/contracts + tests/realtime + tests/test_realtime_*.py: 272 passed, 38 skipped, 1 failed. The one failure was tests/contracts/test_rust_pipeline_indexes_pdf.py::TestRustPipelineIndexesPdf::test_pdf_file_is_stored_as_chunks ("zero pdf chunks"), and the evidence says it is load-flaky, not related to this change: the box was running a large npm install at the time. That test passed alone without the change (1/1) and with it (2/2), and a full rerun of tests/contracts with the change passed 127/127 (4 skipped). On Windows the pool already used spawn, so this change doesn't alter Windows behaviour.

Update after #495 and #487

🤖 Generated with Claude Code, reviewed by FStrasz

The parse pool was created with no mp_context, so on Linux it used fork.
An MCP server creates the pool from a process that already runs threads,
among them the stdio reader holding sys.stdin's lock; a forked worker
inherits that lock held and deadlocks in multiprocessing's _close_stdin()
before running any task. The start-up scan then never processes a file
and the server never reaches query_ready. Measured in a Linux container
(py-spy: every worker in _close_stdin); with a spawn context the same
session was query_ready in 13.8 s.

indexing_coordinator._ensure_mp_start_method() sets the global start
method, but the realtime/MCP path can create the pool before it runs, so
the pool now carries its own spawn context, as batch_processor and the
per-file timeout worker already do.

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

Copy link
Copy Markdown
Collaborator

looks good

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]>
@fstrasz

fstrasz commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

@HananKavitzAmat thanks for the look. I merged main into this branch (173008d, an ordinary merge; nothing rebased). The conflict was #487's initializer=exit_when_parent_dies on the same ProcessPoolExecutor(...) call, and the pool now passes both arguments. The new Update section at the end of the description covers that, what #495 changed about the original reason, and the overlap with #442 and #492.

Upstream's CI test matrix (Ubuntu, macOS, Windows) passed on this tree on my fork before I pushed here.

🤖 Generated with Claude Code

@VakninAriel VakninAriel left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review — PR #493

Verdict: ✅ Approve

Summary

On Linux versions that default to fork, the MCP startup parse pool could inherit a lock held by the stdio reader thread and hang before processing any files. At the reviewed pinned head, this PR explicitly supplies a spawn multiprocessing context when constructing that pool instead of relying on platform defaults or earlier global initialization.

What's good

  • The fix is applied directly at ProcessPoolExecutor construction, so this pool no longer depends on whether another startup path has already set the global multiprocessing method.
  • The new test checks the context actually supplied to the executor and asserts spawn, making the regression detectable even on systems whose default is already spawn.

Freshness warning: Package freshness checked 2026-11-11T15:38:58.574960+00:00: STALE: included heads/eligibility changed; see overview and freshness audit before posting. This report applies only to its pinned SHA.

Severity Count
🔴 Critical 0
🟠 High 0
🟡 Medium 0
🟢 Minor 0

Findings

No retained findings. This is not a guarantee that the code is defect-free.


Review metadata and audit

Draft review for human approval. Nothing has been posted to GitHub.

Pinned head SHA: f28be4db338a0b322cb32bb0f3d5d825198625fb

Package freshness checked 2026-11-11T15:38:58.574960+00:00: STALE: included heads/eligibility changed; see overview and freshness audit before posting. This report applies only to its pinned SHA.

PR: #493

Title: fix(pipeline): parse pool always starts workers with spawn (MCP start-up scan hangs on Linux)

Author: fstrasz · Branches: fix/parse-pool-spawn → main

Diff merge base: 42c5711

Review scope: Full main...HEAD contribution, merge base 42c5711 through pinned head f28be4d; both changed files, module-level worker/config pickling, parse_batch_callback, CLI/MCP entry points and related timeout-worker spawn usage. One commit directly above the merge base. No stacked implementation changes in this diff.

Eligibility: Bug fix addressing existing behavior; no new feature.

Overall assessment (audit only)

No actionable introduced defects found. Explicit spawn context is a minimal fix for fork-after-threads worker startup and preserves lazy pool reuse and task pickling contracts. Isolated threaded pool smoke passed; native integration remains unverified.

Strength evidence

  • {"strength_index": 0, "path": "chunkhound/pipeline_bridge.py", "source": "pr-493.pinned.diff", "detail": "_get_parse_pool passes mp_context=multiprocessing.get_context('spawn') while retaining the existing lazy pool construction."}
  • {"strength_index": 1, "path": "tests/contracts/test_parse_pool_spawn_context.py", "source": "pr-493.pinned.diff", "detail": "test_parse_pool_is_created_with_a_spawn_context captures mp_context and asserts get_start_method() == 'spawn'; it is an explicit-context contract, not an end-to-end deadlock reproduction."}

Verification

  • Reconciler: git -C /tmp/chunkhound-pr-reviews-20261111/pr-493 rev-parse HEAD matched manifest; git diff --check 42c5711 f28be4d and git status --short exited 0 with no output. Both completed reports were read. All assistant-message provider/model/responseModel identities, successful runner statuses, valid provenance, pinned heads and report SHA256 values were checked; two distinct response models confirmed.
  • Astra: timeout 120s env UV_PROJECT_ENVIRONMENT=/home/e119870/views/g/my_chunkhound/.venv PYTHONDONTWRITEBYTECODE=1 uv run --no-sync pytest tests/contracts/test_parse_pool_spawn_context.py tests/contracts/test_parse_pool_reused.py -p no:cacheprovider --basetemp=/tmp/chunkhound-pr-493-astra-a1-5gds8hrz/pytest-pool --timeout=30 -> exit 2, two native-import collection errors, zero tests collected.
  • Astra: timeout 120s env UV_PROJECT_ENVIRONMENT=/home/e119870/views/g/my_chunkhound/.venv PYTHONDONTWRITEBYTECODE=1 uv run --no-sync mypy chunkhound --cache-dir=/tmp/chunkhound-pr-493-astra-a1-5gds8hrz/mypy-cache -> exit 1, 993 errors in 89 files (373 checked). git show 42c5711:chunkhound/pipeline_bridge.py > /tmp/chunkhound-pr-493-astra-a1-5gds8hrz/pipeline_bridge.base.py; repeat with --shadow-file chunkhound/pipeline_bridge.py /tmp/chunkhound-pr-493-astra-a1-5gds8hrz/pipeline_bridge.base.py --cache-dir=/tmp/chunkhound-pr-493-astra-a1-5gds8hrz/mypy-base-cache -> exit 1, byte-identical diagnostics (cmp exit 0).
  • Astra and reconciler: timeout 40s env UV_PROJECT_ENVIRONMENT=/home/e119870/views/g/my_chunkhound/.venv PYTHONDONTWRITEBYTECODE=1 uv run --no-sync python /tmp/chunkhound-pr-493-astra-a1-5gds8hrz/pool_smoke.py -> exit 0. Extracted _get_parse_pool executes work in a spawned child when invoked from a thread with global fork selected, reuses the child, and leaves global start method unchanged.
  • Sonnet reported the same 993 mypy errors in 89 files without baseline comparison, and its spawn/reuse pytest selection failed with two native-import collection errors. No passing native contract claim is made.

Limitations

  • Native-dependent tests did not execute successfully: the shared environment lacks chunkhound_native.chunkhound_native. No native build, dependency installation, network call, source edit or environment repair was performed. CI is only the captured green snapshot, not a fresh run.
  • Supplemental probes extract production definitions and use real external libraries/processes with simplified surrounding state. They are not end-to-end native indexing or MCP verification.
  • The original Linux MCP stdin-lock deadlock and reported query-ready timings were not reproduced. The supplemental smoke does not run the real parser or MCP startup.
  • CI is a captured snapshot, not a fresh run or a guarantee of current branch status.
  • Model identities are verified declarations supplied by the reconciler; the renderer does not inspect raw reports or provenance events.

Disposition of discarded / merged findings

  • Neither reviewer proposed an actionable defect. Discarded throwaway-test atexit registrations as harmless idempotent cleanup, not a user-visible regression.
  • Spawn importability and startup overhead are real constraints but already exercised by existing spawn paths and supported platform entry points; no newly failing supported caller or unacceptable measured overhead was established.
  • PR #492 separately replaces this accessor with a lease-based constructor. If integrating both branches, preserve this explicit spawn choice in the resulting constructor; that overlap is integration advice, not a defect in this pinned diff.

Independent reviewer provenance

  • sonnet — provider: amat-tfy; requested model: standard-chat-dev/claude-sonnet-5.5; verified response model: aws-aidev-models/us.anthropic.claude-sonnet-5-5. Identity evidence · Raw report
  • astra — provider: amat-tfy; requested model: frontier-chat-dev/gpt-6-astra; verified response model: gpt-6-astra. Identity evidence · Raw report

CI snapshot

Captured: 2026-11-11T14:18:23.581921+00:00. Eligibility green: True. Counts: {"SKIPPED": 2, "SUCCESS": 23}

Reviewed by Ariel Vaknin via pi.dev

Brings in the 12 commits merged to main since this branch was last updated, including chunkhound#534's rust-checks fix. No conflicts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolves the conflict with chunkhound#492 (parse-pool idle shrink, 00f4421) in
chunkhound/pipeline_bridge.py. chunkhound#492 removed _get_parse_pool(); the pool is
now created only in _new_parse_pool_locked(), so this PR's spawn
mp_context and its comment move there, and the lease code is upstream's.
tests/contracts/test_parse_pool_spawn_context.py called the removed
_get_parse_pool(): it now creates the pool through a lease and resets it
with _shut_down_parse_pool_at_exit(), as upstream's parse-pool tests do.
Its assertions are unchanged.

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

This branch has not been deployed

No deployments
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.

3 participants