Skip to content

fix(watch): close the rebuild lock unlink-vs-flock race (#4273) - #4277

Closed
ayushcodes10 wants to merge 2 commits into
Graphify-Labs:v8from
ayushcodes10:fix-4273-rebuild-lock-inode-race
Closed

ayushcodes10 wants to merge 2 commits into
Graphify-Labs:v8from
ayushcodes10:fix-4273-rebuild-lock-inode-race

Conversation

@ayushcodes10

Copy link
Copy Markdown
Contributor

Fixes #4273.

flock locks the open file's inode, not the path. Release unlinked the path after unlocking, so a blocked waiter already parked in flock() could end up holding the lock on an orphaned inode with no path at all, while a later caller opened the path fresh, got a brand-new inode that was never locked, and acquired it immediately — two callers running the rebuild at once, writing the same outputs.

Went with the second of the issue's two suggested fixes (keep unlink-on-release, make acquisition robust), since the first (keep the lock file, truncate/rewrite the PID instead) would change the "poll for absence" contract external tooling already relies on:

  1. Reordered release: unlink now happens before flock(LOCK_UN) on the POSIX path. flock only ever wakes a blocked waiter at the unlock call, so doing the unlink first guarantees it has already happened by the time any waiter's flock() can return — this closes the exact gap where a stat-based check could read the path as still valid moments before it disappears under it. (Confirmed this ordering matters empirically: the stat-check alone, with the old unlock-then-unlink order, did not fix the race — the window was still open.)
  2. Stat-based verification on top of the reorder: after flock succeeds, compare the locked file's (st_dev, st_ino) against whatever currently lives at the path; retry against a fresh open() when they differ or the path is gone. Bounded (100 attempts) so a pathological unlink/recreate storm gives up rather than spinning forever, with the same outcome as losing the non-blocking race.

Windows (msvcrt) is untouched — byte-range locks there are tied to an open handle in a way that already prevents this, and the issue is explicitly POSIX-specific.

Verified against the issue's own exact repro script (multiprocessing, no git repo needed): before, C reliably entered while B still held the lock across every run; after, C only enters once B exits, across every run. New regression test in tests/test_watch.py mirroring the same repro shape, confirmed to fail reliably against pre-fix code (3/3 runs) and pass reliably with the fix (3/3 runs). Full suite passes (only the one pre-existing, unrelated Erlang cross-file-call gap, visible now that tree-sitter-language-pack is installed in this environment).

flock locks the open file's inode, not the path. Release unlinked the
path after unlocking, so a blocked waiter already parked in flock
could end up holding the lock on an orphaned inode with no path at
all, while a later caller opened the path fresh, got a brand new
inode that was never locked, and acquired it immediately. Two callers
then ran the rebuild at once and wrote the same outputs.

Reorder release so the unlink happens before the unlock on the fcntl
path: flock only ever wakes a blocked waiter at the unlock call, so
doing the unlink first guarantees it has already happened by the time
any waiter's flock call can return, closing the gap where a stat
based check could read the path as still valid moments before it
disappeared under it.

Add the stat based check itself as a safety net on top of the
reorder: after flock succeeds, compare the locked file's device and
inode against whatever currently lives at the path, and retry against
a fresh open when they differ or the path is gone. Bounded so a
pathological unlink and recreate storm gives up rather than spinning
forever, with the same outcome as losing the non blocking race.

Windows locking is untouched: byte range locks there are tied to an
open handle in a way that already prevents this, and this issue is
POSIX specific.

Fixes Graphify-Labs#4273.
Mirrors the issue's own repro through multiprocessing: a brief
holder, a blocking waiter that acquires once the holder releases,
and a third caller arriving after release while the waiter still
holds. The third caller must never enter while the waiter is inside.
Confirmed to fail reliably against pre fix code across several runs,
and to pass reliably with the fix across several more.
@github-actions

Copy link
Copy Markdown

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

@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.

Formal verification could not match the changed code to the code graph, so it may not have checked the code this PR changed.

Worth a look — the grounded gate found no coupling regressions or blocking issues, but 2 advisory finding(s) below merit a look before merge.

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 a POSIX race in _rebuild_lock (#4273) where a waiter blocked in flock() could end up holding a lock on an orphaned inode after release unlinked the path, letting a later caller lock a fresh file and enter the rebuild concurrently. After acquiring, the lock now confirms that its fd still matches the file at the lock path and retries if not, returning not-acquired after 100 mismatches. Release unlinks the path before unlocking so a woken waiter's check always sees the unlink.

Worth a look

  • Blocking rebuild lock can yield False and enter the protected body — graphify/watch.py:254 · Escalate · medium
    • agreed by 2 of 2 members but NOT verified (no proof, no reproducing execution) — consensus is not a verdict; needs human review
  • Non-blocking stale-lock miss raises on context exit instead of returning False cleanly — graphify/watch.py:242 · Escalate · medium
    • 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 — 695 functions depend on the 461 functions this change touches.

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

  • _rebuild_code() — 162 callers, 56 callees (high)
  • main() — 103 callers, 3 callees (high)
  • dispatch_command() — 2 callers, 130 callees (high)
  • _run_cli() — 8 callers, 8 callees (high)
  • _reconcile_graph_html() — 8 callers, 5 callees (high)
  • watch() — 5 callers, 7 callees (medium)
  • _reconcile_existing_graph() — 1 callers, 11 callees (medium)
  • _reconcile_markdown_links() — 1 callers, 7 callees (medium)
  • …and 1 more

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

Test selection

Test selection

17 of 361 test file(s) selected (5%) via static blast radius.

  • tests/test_cache_stale_import_target.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_incremental_unscanned_placeholders.py — impact
  • tests/test_kotlin_grammar.py — impact
  • tests/test_labeling.py — impact
  • tests/test_markdown_code_spans.py — impact
  • tests/test_no_cluster_undeclared_endpoints.py — impact
  • tests/test_python_inherited_method_calls.py — impact
  • tests/test_skill_persisted_scan_options.py — impact
  • tests/test_terraform_modules.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.

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 pushed a commit that referenced this pull request Oct 10, 2026
Landed from PR #4277 (squashed, authorship preserved).
@safishamsi

Copy link
Copy Markdown
Member

Landed in 0.9.85 (now on PyPI) via an authorship-preserving cherry-pick, so your commit authorship is retained. Thank you, @ayushcodes10!

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.

_rebuild_lock unlinks .rebuild.lock on release, so two rebuilds can hold the lock at once (POSIX)

2 participants