Skip to content

fix(watch): don't wait forever when msvcrt.locking is unsupported (#4924) - #4926

Closed
AndresDiagoM wants to merge 1 commit into
Graphify-Labs:v8from
AndresDiagoM:fix/win-rebuild-lock-unsupported-fs
Closed

AndresDiagoM wants to merge 1 commit into
Graphify-Labs:v8from
AndresDiagoM:fix/win-rebuild-lock-unsupported-fs

Conversation

@AndresDiagoM

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #4924.

On Windows, _rebuild_lock treated every OSError from msvcrt.locking as "another rebuild holds the lock". On a filesystem that cannot byte-range lock at all, such as the WSL share \\wsl.localhost\<distro>\... (where the call raises EINVAL every time), that made the blocking graphify update CLI (block_on_lock=True) sleep in a while True forever. The non-blocking git hooks queued their change and returned "Rebuild already in progress", with no lock holder ever left to drain the queue.

The Windows branch now looks at the errno:

  • EACCES / EDEADLK is contention, and behaves as before: blocking callers wait and retry every 50 ms, non-blocking callers get False.
  • Any other errno is treated as "locking unsupported on this filesystem". The rebuild proceeds without a lock (yield True, like the existing fallback when neither fcntl nor msvcrt exists), and a warning on stderr names the lock file.

In that unsupported case the caller does not take over .rebuild.lock: it writes no PID into it and does not unlink it on release. On a WSL-share repo the file can belong to a WSL-side flock holder. Overwriting its PID, or unlinking the path, would let the next WSL-side caller flock a fresh inode while the first is still rebuilding (the #4273 race). If this call created the file only to try the lock, it removes that still-empty file again before the rebuild starts, so no stale lock is left behind (GH-858).

The POSIX branch is unchanged.

Type of change

  • Bug fix

Verification & Invariants

Invariants:

  1. A rebuild waits on _rebuild_lock only for another holder, never for a condition that cannot change.
  2. Contention keeps its existing semantics.
  3. A caller that could not take the lock never modifies or removes a lock file another process may hold.
  • Read the CONTRIBUTING.md guide.
  • Reproduced the issue and identified the invariant.
  • Made the smallest fix necessary.
  • Added a regression test.
  • Kept the PR description synchronized with the final implementation.
  • Documented any limitations / unsupported cases explicitly.

Tests, in tests/test_watch.py. They run the msvcrt branch on any OS through a stand-in msvcrt, with fcntl hidden. time.sleep is replaced so a loop that never ends fails the test instead of hanging it.

Test origin/v8 this PR
test_rebuild_lock_blocking_does_not_spin_when_filesystem_cannot_lock fails passes
test_rebuild_lock_non_blocking_proceeds_when_filesystem_cannot_lock fails passes
test_rebuild_lock_unsupported_filesystem_leaves_another_holders_lock_file_alone[True/False]: a pre-existing .rebuild.lock with a foreign PID must keep its content and inode fails passes
test_rebuild_lock_blocking_still_waits_while_msvcrt_lock_is_contended passes passes
test_rebuild_lock_non_blocking_still_yields_false_while_msvcrt_lock_is_contended passes passes

The "leaves another holder's lock file alone" test also fails on an earlier version of this branch, which wrote its PID and unlinked the file in the unsupported case. I checked that the real two-OS race it guards against exists:

  • WSL process L1 holds the flock.
  • A Windows process runs on the same graphify-out through \\wsl.localhost.
  • A second WSL process L2 then tries the lock.

With that earlier version, L2 got the lock while L1 still held it. With this PR, L1's PID and the file stay intact, and L2 gets False until L1 releases.

Limitations:

How was this tested?

On Windows, uv run could not fetch dependencies on this machine (TLS interception: invalid peer certificate: UnknownIssuer). The Windows runs therefore used an existing venv of this project (CPython 3.13.5) with PYTHONPATH pointed at this branch; I checked that graphify.watch was imported from this branch. The Linux runs used uv run in WSL (CPython 3.10.12, separate venv).

# lock tests, Windows, Python 3.13.5
python -m pytest tests/test_watch.py -q -k rebuild_lock
  this PR:    11 passed, 1 skipped
  origin/v8:  4 failed (the regression tests above), 7 passed, 1 skipped

# lock tests, Linux (WSL), Python 3.10.12
uv run python -m pytest tests/test_watch.py -q -k rebuild_lock
  this PR:    12 passed
  origin/v8:  4 failed (same 4), 8 passed

# all of tests/test_watch.py, Linux:  205 passed, 3 skipped

# full suite, Windows, Python 3.13.5
python -m pytest tests -q
  this PR:    12 failed, 8983 passed, 286 skipped, 1 xfailed
  origin/v8:  18 failed, 8971 passed, 286 skipped, 1 xfailed
  # no failure is new: all 12 also fail on origin/v8 (Windows-environment failures,
  # e.g. test_absolute_path_lookup_skips_getcwd, test_ext_tagged_id_clash.py).
  # The other 6 base failures, test_copied_ast_cache_preserves_terraform_ids[parallel-*],
  # passed in this run but failed in an earlier run of this branch too, so they look flaky.
  # 8983 = 8971 + the 6 new tests + those 6.

# lint / types on the touched files (Linux)
uv run ruff check graphify/watch.py tests/test_watch.py   -> All checks passed!
uv run pyright graphify/watch.py   -> 12 errors, all already on origin/v8 (which has 20);
                                      none new

# real case: `graphify update . --force` from Windows on a 40-file repo under \\wsl.localhost
#   before (installed 0.9.94, same lock code): hung, killed after 240 s with 0.7 s of CPU
#   after (this branch): prints the warning, gets past the lock, and the whole run ends in ~10 s
#   (exit 1 on #4925)

Graphify-specific checklist

  • I updated generated skill artifacts — n/a, no skill fragments touched.
  • I confirmed that AST/structural extraction remains deterministic (not touched: the change is limited to _rebuild_lock).
  • I reviewed changes for security implications.
  • I confirmed no API keys or local-only graph data are included.
  • I disclosed AI authorship in my commit messages.

On Windows, _rebuild_lock treated every OSError from msvcrt.locking as
"another rebuild holds the lock". On a filesystem that cannot byte-range
lock at all (the WSL share \wsl.localhost raises EINVAL on every call),
the blocking `graphify update` CLI slept in a `while True` forever, and
the non-blocking git hooks queued their change and returned, with no lock
holder ever left to drain the queue.

Only EACCES/EDEADLK now count as contention. Any other errno is treated
as "locking unsupported": the rebuild proceeds without a lock and a
warning on stderr names the lock file. That path does not take over
.rebuild.lock (no PID write, no unlink on release), because a WSL-side
flock holder may own it and unlinking it would let a second WSL-side
caller in on a fresh inode (Graphify-Labs#4273). An empty lock file this call created
only to probe the lock is removed again before the rebuild.

Fixes Graphify-Labs#4924

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

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

Thanks for the pull request, @AndresDiagoM. 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.

@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Nov 1, 2026

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Graphify reviewed this change.

No issues found by static checks — no coupling regressions or blocking issues in the code graph. That is not the same as safe to merge: see what was not checked.

Not checked: tests were not run; formal verification proved 0 of 1 changed function(s) (1 not verified).

Formal verification. PR-changed functions: 0/1 verified (0 proven, 0 may-equivalent, 0 distinguished) · 1 not verified (1 vacuous).

Not verified on this run: \_rebuild\_lock (vacuous: never exercised).


Graphify review — findings

Fixes _rebuild_lock hanging or never rebuilding on Windows when msvcrt.locking can't take a byte-range lock at all, such as the \\wsl.localhost share (#4924). Only contention errnos (EACCES/EDEADLK) are retried now, with blocking callers polling and non-blocking callers yielding False; any other error warns on stderr and proceeds unlocked, so rebuilds on that filesystem are no longer serialized. That fallback leaves a pre-existing .rebuild.lock untouched since a WSL-side flock holder may own it, and only unlinks the file if this call created it and it is still empty, which is the cleanup that could remove a file another process just created.

No blocking issues surfaced. 2 lower-confidence candidates did not survive cross-model review.

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 890 functions depend on the 543 functions this change touches.

Health — grade A; 10 existing hotspot(s) in the area this change touches (pre-existing, not introduced here):

  • _rebuild_code() — 213 callers, 67 callees (high)
  • dispatch_command() — 5 callers, 142 callees (high)
  • main() — 121 callers, 3 callees (high)
  • _run_cli() — 11 callers, 8 callees (high)
  • watch() — 6 callers, 11 callees (high)
  • _check_shrink() — 17 callers, 3 callees (high)
  • _reconcile_graph_html() — 8 callers, 5 callees (high)
  • _inputs_unchanged() — 4 callers, 4 callees (medium)
  • …and 2 more

Verification — 890 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: 634 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

31 of 487 test file(s) selected (6%) via static blast radius.

  • tests/test_cache_stale_import_target.py — impact
  • tests/test_compact_graph_json.py — impact
  • tests/test_csharp_method_owner_labels.py — impact
  • tests/test_elixir_import_resolution.py — impact
  • tests/test_elixir_qualified_calls.py — impact
  • tests/test_elixir_unqualified_call_scope.py — impact
  • tests/test_export_direction.py — impact
  • tests/test_external_stub_endpoints.py — impact
  • tests/test_go_receiver_member_calls.py — impact
  • tests/test_incremental_unscanned_placeholders.py — impact
  • tests/test_java_member_calls.py — impact
  • tests/test_kotlin_grammar.py — impact
  • tests/test_labeling.py — impact
  • tests/test_language_override_lifecycle.py — impact
  • tests/test_lost_saved_semantic_nodes.py — impact
  • tests/test_markdown_code_spans.py — impact
  • tests/test_no_cluster_undeclared_endpoints.py — impact
  • tests/test_pr10_watch_overrides.py — impact
  • tests/test_python_inherited_method_calls.py — impact
  • tests/test_python_injected_factory_calls.py — impact
  • tests/test_python_property_reads.py — impact
  • tests/test_python_pytest_fixture_receivers.py — impact
  • tests/test_rust_use_trees.py — impact
  • tests/test_skill_persisted_scan_options.py — impact
  • tests/test_solidity_inherited_calls.py — impact
  • tests/test_terraform_modules.py — impact
  • tests/test_undirected_two_file_import_cycle.py — impact
  • tests/test_update_no_change_gate.py — impact
  • tests/test_watch.py — impact, changed-test
  • tests/test_watch_identity_cache.py — impact
  • tests/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.

Docs that may be stale (advisory)

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

@safishamsi

Copy link
Copy Markdown
Member

Landed in 0.9.101 (published to PyPI), cherry-picked with your authorship preserved. Fixes #4924. Thanks @AndresDiagoM.

@safishamsi safishamsi closed this Nov 2, 2026
@AndresDiagoM
AndresDiagoM deleted the fix/win-rebuild-lock-unsupported-fs branch November 3, 2026 05:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor Pull request from a returning community contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

graphify update waits forever on Windows when the filesystem has no byte-range locks (e.g. \wsl.localhost)

2 participants