Skip to content

fix(platform): keep platform off WMI so spawned workers keep their handles - #526

Merged
HananKavitzAmat merged 3 commits into
chunkhound:mainfrom
fstrasz:fix/platform-wmi-guard
Nov 17, 2026
Merged

HananKavitzAmat merged 3 commits into
chunkhound:mainfrom
fstrasz:fix/platform-wmi-guard

Conversation

@fstrasz

@fstrasz fstrasz commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Problem

On Windows with Python 3.12, an MCP server's initial scan can fail while the machine is under CPU load. A parse-pool worker dies with OSError: [WinError 6] The handle is invalid at self._sem.release() in multiprocessing/queues.py, while releasing its call-queue semaphore. The scan then fails with RustPipelineError (BrokenProcessPool).

The same lost handle can instead make a worker release the wrong semaphore, and the pool then stalls with no error. We saw that once, on a build with other parse-pool changes, but not in the runs on main below.

We reproduced it with a copy of a real index whose start-up scan overlaps an HNSW index build, which loads the CPU from 35-45 s after the start. On main, with Python 3.12.13, 2 of 2 server starts failed this way.

Root cause

On Python 3.12, platform.system(), win32_ver(), machine() and processor() ask WMI first, through CPython's private _wmi module. Under high load the WMI query can time out. The query thread then outlives the call and later calls CloseHandle() on a value it reads from the caller's finished stack frame. That can close an unrelated handle in the process.

This is CPython gh-130727. Its fix, python/cpython#134313 ("Fix a race in internal calls into WMI that can result in an 'invalid handle' exception under high load"), shipped in 3.13.4 and 3.14.0b2. 3.12 got no fix.

ChunkHound calls platform.system() at import, in chunkhound/utils/windows_constants.py. So every spawned parse worker runs a WMI query while it starts, which is exactly when the scan loads the CPU.

What the evidence shows on the reproduction:

  • Proven: the parent's pool handles never change. The worker's handle is closed inside the worker, during its imports.
  • Strongly indicated: the close happens inside platform's WMI query. Keeping platform off WMI removes the failure.
  • Not reproduced: the native close itself. It matches gh-130727's analysis.

Fix

disable_platform_wmi_queries() (new chunkhound/platform_guard.py) runs from chunkhound/__init__.py, before anything calls platform. It therefore runs in every ChunkHound process, including spawned workers, which re-import the package.

On Windows, on Python releases that have the bug (before 3.13.4), it replaces platform._wmi_query with a function that raises OSError. platform already handles that error: it falls back to sys.getwindowsversion(), cmd /c ver and environment variables.

It does nothing in these cases:

  • off Windows;
  • on 3.13.4 and later, and on 3.14;
  • where platform has no _wmi_query (before 3.12). There it logs at debug level.

The module imports only the standard library. It runs at package import, which also happens in build environments that don't have ChunkHound's dependencies.

Alternatives considered:

  • Use sys.platform == "win32" in windows_constants.py instead of platform.system(). That removes the workers' only query at import, but the daemon still calls platform at runtime:
    • the Watchman runtime loader calls platform.system() and platform.machine();
    • the OpenAI and Anthropic SDKs call platform.platform() for their request headers.
      So the daemon's first WMI query would only move from import into realtime start-up, under the scan's load.
  • Apply the guard on every Python version. CPython is fixed from 3.13.4, so it would replace a private standard-library function there for no benefit.

Tests

tests/test_platform_wmi_guard.py runs each check in a fresh interpreter, because platform caches its answers and the guard acts at import:

Test Expected On main
test_a_chunkhound_process_never_queries_wmi_through_platform (Windows, Python before 3.13.4) 0 WMI queries after import chunkhound and calls to platform.win32_ver(), machine() and processor() assert '4' == '0': 4 WMI queries
test_platform_answers_are_unchanged_by_importing_chunkhound system(), release(), version(), machine(), win32_ver(), processor() and platform() give the same answers with and without the guard Passes. It checks that the fallback reports the same values.

Upstream CI runs Python 3.14. There the first test skips and the guard does nothing. Both tests were run on Windows with Python 3.12.13.

Mutations: removing the guard call, or gating it at (3, 12, 0), each turns the first test RED ('4' == '0').

We ran the MCP, daemon and realtime test files (44 files) and the new test file serially, on Windows with CPython 3.12.13:

Build Failed Passed Skipped
main 8 435 40
this PR (including the 2 new tests) 10 435 40

The 2 extra failures are test_mcp_initialization_timeout_on_large_directory and test_watchdog_recursive_blocking_on_deep_directory_tree. Both fail on initialize timing out after 10-15 s on this machine. They fail on main as well: we reran their two files twice on each build, alternating, and both builds gave 5 failed / 1 passed every time, with both tests failing.

The fallback is not slower. In a fresh interpreter, platform.system() took 0.14 s with the guard and 0.25-0.42 s through WMI. win32_ver() and platform() took about 0.1 s either way.

ruff and mypy report the same findings as main on the changed files.

Upstream's CI workflow, run on our fork at this head (bb0254e), passed every job except rust-checks, including all 9 test shards on Linux, macOS and Windows: https://github.com/fstrasz/chunkhound/actions/runs/43350082315. rust-checks fails on every OS until #524 is resolved: Rust 1.100.0 stopped the Rust test binaries from loading libduckdb. This PR changes no Rust code.

Measurements

These runs used the reproduction above on Windows with CPython 3.12.13. Each start used a fresh copy of the same index (79 files), and the starts ran one after another.

Build Starts Result
main (24752d0) 2 Both scans failed with BrokenProcessPool, at 51 s and 46 s, after 1 and 2 [WinError 6] errors in workers.
this PR 3 All scans completed (79 files, 3,447 chunks) in 127-147 s. No WinError 6, no traceback, no hang.

In every run, on both builds, the HNSW build loaded the CPU from about 35-45 s after the start. On main, the errors came during that window.

Not reproduced:

  • macOS and Linux, where the guard does nothing;
  • Python 3.13 and later.

Overlap

This PR is standalone on main and depends on no open PR.

#426, #521 and #522 merge cleanly with main today. Each conflicts with this PR only in CHANGELOG.md: they add adjacent [Unreleased] entries, so keep both. Whichever merges second rebases. #426 also edits chunkhound/__init__.py, but those edits merge cleanly with this PR's.

#455, #442 and #392 also edit CHANGELOG.md, and they already conflict with main.

CHANGELOG.md: [Unreleased] → Fixed.

🤖 Generated with Claude Code, reviewed by FStrasz

…ndles

On Python 3.12, platform.system()/win32_ver()/machine()/processor() query
WMI through CPython's _wmi module. After the WMI connect times out (under
CPU load), the query thread keeps using a pointer into the caller's
finished stack frame and can CloseHandle() an arbitrary handle in the
process: CPython gh-130727, fixed by gh-134313 in 3.13.4 and 3.14.0b2; 3.12
got no fix. ChunkHound calls platform.system() at import time
(chunkhound/utils/windows_constants.py), so every spawned parse worker ran
a WMI query while starting. On a reproducible case (an index whose start-up
scan runs while the HNSW build loads the CPU), workers lost their
call-queue semaphore handle a few seconds later and died with WinError 6, or
released the wrong semaphore and stalled the pool with no error.

disable_platform_wmi_queries() runs from chunkhound/__init__.py, so in every
ChunkHound process, spawned workers included: on Windows, on a Python before
3.13.4, it replaces CPython's private platform._wmi_query with one that
raises OSError, which platform already handles by falling back to
sys.getwindowsversion(), `cmd /c ver` and environment variables. On the
fixed Pythons it does nothing, and it can go when Python 3.12 support ends.
Where _wmi_query does not exist it logs at debug level and does nothing. It
imports only the standard library, since build environments import the
package without ChunkHound's dependencies.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fstrasz and others added 2 commits November 15, 2026 10:21
Brings in chunkhound#521. CHANGELOG.md conflicted only because both PRs add a line at the top of [Unreleased] ### Fixed; both entries are kept, this PR's first. No other conflicts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Brings in chunkhound#510, chunkhound#504 and chunkhound#534. No conflicts.

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

Copy link
Copy Markdown
Collaborator

Looks good

@HananKavitzAmat
HananKavitzAmat added this pull request to the merge queue Nov 17, 2026
Merged via the queue into chunkhound:main with commit ec20188 Nov 17, 2026
25 checks passed
fstrasz added a commit to fstrasz/chunkhound that referenced this pull request Nov 17, 2026
Brings in chunkhound#526, chunkhound#502 and chunkhound#533. CHANGELOG.md conflicted only because this PR and chunkhound#526 each add a line at the top of [Unreleased] ### Fixed; both entries are kept, this PR's first. No other conflicts.

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