Repository navigation
fix(platform): keep platform off WMI so spawned workers keep their handles - #526
Merged
Merged
Conversation
…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]>
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]>
Collaborator
|
Looks good |
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]>
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.
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 invalidatself._sem.release()inmultiprocessing/queues.py, while releasing its call-queue semaphore. The scan then fails withRustPipelineError(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()andprocessor()ask WMI first, through CPython's private_wmimodule. Under high load the WMI query can time out. The query thread then outlives the call and later callsCloseHandle()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, inchunkhound/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:
platform's WMI query. Keepingplatformoff WMI removes the failure.Fix
disable_platform_wmi_queries()(newchunkhound/platform_guard.py) runs fromchunkhound/__init__.py, before anything callsplatform. 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_querywith a function that raisesOSError.platformalready handles that error: it falls back tosys.getwindowsversion(),cmd /c verand environment variables.It does nothing in these cases:
platformhas 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:
sys.platform == "win32"inwindows_constants.pyinstead ofplatform.system(). That removes the workers' only query at import, but the daemon still callsplatformat runtime:platform.system()andplatform.machine();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.
Tests
tests/test_platform_wmi_guard.pyruns each check in a fresh interpreter, becauseplatformcaches its answers and the guard acts at import:test_a_chunkhound_process_never_queries_wmi_through_platform(Windows, Python before 3.13.4)import chunkhoundand calls toplatform.win32_ver(),machine()andprocessor()assert '4' == '0': 4 WMI queriestest_platform_answers_are_unchanged_by_importing_chunkhoundsystem(),release(),version(),machine(),win32_ver(),processor()andplatform()give the same answers with and without the guardUpstream 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:
The 2 extra failures are
test_mcp_initialization_timeout_on_large_directoryandtest_watchdog_recursive_blocking_on_deep_directory_tree. Both fail oninitializetiming 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()andplatform()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-checksfails 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.
BrokenProcessPool, at 51 s and 46 s, after 1 and 2[WinError 6]errors in workers.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:
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 editschunkhound/__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