Repository navigation
fix(watch): serialize _rebuild_lock on Windows via msvcrt (#3881) - #3883
harshaygadekar wants to merge 1 commit into
Conversation
Closes Graphify-Labs#3881. On Windows, _rebuild_lock falls back to yield True when fcntl cannot be imported, leaving concurrent rebuilds completely unserialized and causing file collisions on .graph.tmp.json. Implement Windows file locking using Python's standard library msvcrt module. Lock a byte range at offset 4096 so external readers can still read the owning PID at byte 0 without hitting Windows mandatory lock PermissionErrors. Un-skip the 4 rebuild lock tests and 3 pending queue tests on Windows, and add test_rebuild_lock_blocks_concurrent_process.
|
Thanks for the pull request, @harshaygadekar. A maintainer will review it soon. Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions. A couple of things that speed up review: make sure the test suite passes on Python 3.10 and 3.13, and that the change keeps extraction deterministic. |
There was a problem hiding this comment.
Graphify reviewed this change.
Worth a look — the grounded gate found no coupling regressions or blocking issues, but 1 advisory finding(s) below merit a look before merge.
Formal verification. No changes could be formally verified in this run.
Graphify review — findings
Adds Windows support to the per-repo rebuild lock in _rebuild_lock, using msvcrt.locking at byte offset 4096 (so external pollers can still read the PID at byte 0) when fcntl is unavailable, and only falls back to a no-op yield(True) when neither is present. Removes the win32 skips from the lock tests and adds test_rebuild_lock_blocks_concurrent_process, which spawns a subprocess to verify a second process can't acquire a held lock non-blocking.
Worth a look
- Releaser can unlink a lock file after another process has acquired it —
graphify/watch.py:238· Escalate · high- agreed by 2 of 2 members but NOT verified (no proof, no reproducing execution) — consensus is not a verdict; needs human review
Analysis details — impact, health, verification
Impact & health
Graphify review
Impact — 640 functions depend on the 446 functions this change touches.
Health — this change adds coupling hotspots:
- new:
_rebuild_code()— 146 callers, 55 callees - new:
main()— 98 callers, 3 callees - new:
dispatch_command()— 2 callers, 125 callees - new:
watch()— 5 callers, 7 callees - new:
_reconcile_graph_html()— 6 callers, 5 callees - new:
_reconcile_existing_graph()— 1 callers, 8 callees - new:
_reconcile_markdown_links()— 1 callers, 7 callees - new:
test_poisoned_manifest_is_healed()— 0 callers, 6 callees
Verification — 640 functions in the blast radius were not formally verified this run (proofs are advisory here).
Gate & verification
graphify gate
PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.
Advisory (not blocking):
- verification_scope: 468 function(s) in the blast radius were not formally verified this run
Test selection
Test selection
8 of 302 test file(s) selected (3%) via static blast radius.
tests/test_elixir_import_resolution.py— impacttests/test_external_stub_endpoints.py— impacttests/test_kotlin_grammar.py— impacttests/test_labeling.py— impacttests/test_markdown_code_spans.py— impacttests/test_terraform_modules.py— impacttests/test_watch.py— impact, changed-testtests/test_watch_manifest_location.py— impact
Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.
Formal verification
Could not verify: Could not verify \_rebuild\_lock.
The verifier did not have enough to check \_rebuild\_lock, so it is saying so rather than guessing. No false assurance is the whole point.
Guarantee: No guarantee either way, this is an honest abstention, not a pass.
Note: Reason: no capturable inputs from the test suite; property tier: not verifiable: all 9 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous
· 8 more finding(s) on lines outside this diff (see the check run).
Security: close the Fortran cpp #include arbitrary-file-read (GHSA-pcc4-rvhr-2pr8), the last Aider/Devin monolith --watch shell sink (#3852), and terraform name/value secret redaction (#3870). Plus Windows watch rebuild locking (#3883), C# tuple element-name refs (#3877), JSX component-usage calls (#3855), and nested scan-root Python import projection (#3867). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Shipped in v0.9.70 (now on PyPI) via an authorship-preserving cherry-pick, so your commit keeps contributor-graph credit. Thanks @harshaygadekar! Serializes rebuilds on Windows via msvcrt byte-range locking; POSIX fcntl path unchanged. |
Closes #3881.
Problem
On Windows,
_rebuild_lockingraphify/watch.pycatchesImportErrorwhen importingfcntland immediately falls back toyield True. Becausefcntldoes not exist on Windows, every caller is handed the lock simultaneously.When git hooks (like
post-commitandpost-checkoutback-to-back) or a background watcher and an explicitgraphify updaterun at roughly the same time on Windows, they execute concurrently. Both write to.graph.tmp.jsonwithout synchronization and attemptos.replaceontograph.json, causingPermissionError: [WinError 32], truncated JSON files, or mismatched community labels across rebuilds.The original lock issue (#1037) was reported on Windows, but only the POSIX
fcntl.flockhalf landed. Additionally, four lock tests intests/test_watch.pyand three pending queue tests were markedskipif(sys.platform == "win32"), so Windows CI never ran the locking test suite.RCA
In
graphify/watch.py:On Windows,
fcntlis never available. The early return bypassed lock creation, acquisition, PID writing, and cleanup entirely.Fix
Stdlib Windows file locking (
msvcrt.locking):When
fcntlis unavailable, use Python's built-inmsvcrtmodule on Windows.blocking=False): Callsmsvcrt.locking(fh.fileno(), msvcrt.LK_NBLCK, 1). If already locked, raisesOSError, caught to yieldFalse.blocking=True): Loops with a short 50ms sleep untilLK_NBLCKsucceeds, avoidingmsvcrt.LK_LOCK's 10-second timeout.msvcrt.locking(fh.fileno(), msvcrt.LK_UNLCK, 1)infinally:.Byte offset past PID payload (
_win_lock_offset = 4096):Windows byte-range locks are mandatory. If byte 0 is locked, external pollers attempting to read the holder PID from
.rebuild.lockhitPermissionError: [Errno 13]. Locking 1 byte at offset 4096 preserves concurrent read access to byte 0 while enforcing strict mutual exclusion.Enable tests on Windows:
Removed
skipif(win32)from the 4 lock tests and 3 pending queue tests intests/test_watch.py, and addedtest_rebuild_lock_blocks_concurrent_processto assert that a second process is rejected when the lock is held.Verification/Test Suite
Before fix (Windows 11, Python 3.14):
Running a holder subprocess with a concurrent non-blocking caller:
After fix (Windows 11, Python 3.14):
Test suite: