Skip to content

fix(lock): deadline rethrow = never saw the lock file, not last-probe-absent (#107) - #114

Merged
abogartz merged 4 commits into
abogartz:mainfrom
WhollyMindless:fix/lock-absent-holder-aware
Oct 26, 2026
Merged

abogartz merged 4 commits into
abogartz:mainfrom
WhollyMindless:fix/lock-absent-holder-aware

Conversation

@WhollyMindless

Copy link
Copy Markdown
Contributor

Fixes #107, exactly your prescription, with one precision about WHERE everSawHolder gets set.

The fix

lockFileAbsent's boolean quietly merges two different answers a probe can give - "stat succeeded" and "stat failed with something that isn't ENOENT" - and the else branch resets strikes on both. Three-state now:

  • probe present -> everSawHolder = true, strikes reset (as today)
  • probe ENOENT -> strikes++ , the 8-strike throw untouched (as today)
  • probe unknown (non-ENOENT stat label) -> strikes reset (as today), everSawHolder NOT set

and the deadline branch becomes if (!everSawHolder) throw firstErr. Setting the flag in the else branch as literally worded would let consequence 1's blind-probe writer keep proceeding unguarded - "set when a probe finds the lock" is stat succeeding, and only that.

Both consequences then close as designed: consequence 2's healthy vanish-at-deadline race falls through to the proceed-unlocked concession (everSawHolder is what "this whole time" means); consequence 1's never-saw-anything writer finally meets the comment's promise at the deadline instead of the guard's absence.

Pins

holdFileLock grows a final optional now: () => number = Date.now seam (exported; same "test seam" shape as resetHitThrottle/resetCaptureCooldowns, production call sites untouched) and a mock.module on node:fs/promises intercepts ONLY the exact lock paths a test scripts - everything else passes through to the captured pre-mock functions. No pin depends on a wall-clock race:

  • consequence 2, red pre-fix: iteration 1 probes the live holder (present); iteration 2's create still fails, the scripted open unlinks right after failing, the scripted clock expires the budget in that iteration. Pre-fix: Expected: false, Received: true on threw - the rethrow ate an ordinary LOCK_WRITE. Post-fix the concession runs: timeout audited, write lands unlocked.
  • consequence 1, red pre-fix: create always EPERMs, stat always answers EACCES - strikes can never leave 0, and pre-fix the deadline branch took the concession with nothing ever proven. ran === true pre-fix (the unguarded write the comment forbids); post-fix the EPERM surfaces.
  • regression pin, green both sides: probes that prove ENOENT keep the strike rule in charge - eight strikes end the wait with the clock never advancing at all.

Verified red (2 red / 1 green) by reverting only src/lock.ts with the seam shimmed through withStoreLock; green 3/3 after.

Honest bounds

  • Your measurement note said the false-positive tail was reasoned about, not reached on macOS - same here: these are proofs by construction of the two branches, not a reproduced production loss. The construction is exact (same call order as the loop: create, probe, steal-probe, deadline), which is the point of driving it instead of racing it.
  • Two full-suite runs early in verification went red on concurrency.test.ts time-window tests under machine load and never reproduced: 4 further quiet full-suite Windows runs 486/0, 2 quiet main-baseline runs 483/0, both Linux legs green under both Buns. Reporting it rather than burying it.

Verified

Bun 1.3.10 Bun 1.4.4
Windows full suite 486 pass / 0 fail (5 skips) x4 quiet lock area 13/13
Linux (WSL2 clone of the pushed branch, full bun run test) 491 tests / 0 fail (8 skips) 491 tests / 0 fail

tsc --noEmit and biome lint clean. The pre-existing Windows LOCK_WRITE/ACL tests (lock.test.ts 10/10 incl. the icacls case) pass untouched - the 8-strike path, the steal path, and both timeout policies are behaviorally unchanged.

assisted by Qwen3-flash-next

@WhollyMindless

Copy link
Copy Markdown
Contributor Author

The lone test-windows red is not this branch's test — the run log shows why, and it is worth recording:

(fail) supersede racing appends > a concurrent reader never sees a half-written file [113.73ms]

If you want the family quieted rather than triaged per-run, the #105 shape (one test-level timeout pin per heavy window test, defaults untouched) is a five-minute PR covering both: scenarios-consistency.test.ts:529 and the two racing-append windows here. Say the word.

@abogartz abogartz left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Verdict: APPROVED

Red leg reproduced exactly as described. I reverted only the deadline condition on this branch (!everSawHolder → absentStrikes > 0): consequence-2 test red (Expected: false, Received: true on threw), consequence-1 test red (ran true — the unguarded write), regression pin green both sides. Restored: 3/3 green. Full suite on macOS: 491 pass / 8 skip / 0 fail, tsc --noEmit clean.

The three-state probe is right, and your deviation from #107's literal wording is the correct reading. #107 said "set everSawHolder in the else branch" — doing that would set the flag on unknown probes too and re-open consequence 1 exactly (a blind filesystem would concede "unlocked-but-never-proven"). Setting it only on stat success makes the code match the comment's promise: "seen the lock file not even once". Your body called this out; I verified both branch tables (present/ENOENT/unknown × strikes/flag/deadline) land where the comment says.

Behavioral mapping, checked branch by branch: holder-entire-time → unchanged concession; seen-then-vanished → now concedes (the fix); only-ENOENT → 8-strike throw unchanged (ABSENT_THROW_STRIKES = 8, lock.ts:37); only-unknown → now throws firstErr at the deadline where the old code conceded — a deliberate tightening that only affects writers whose stat never once answered, which is the case the comment always said must not write. No call site regressed: one production caller (lock.ts:261, 5-arg, real clock), lockFileAbsent has zero remaining references.

Test construction: driving order matches the real loop (create → probe → steal-probe → deadline), the fake clock is bounded (the stepClock clamp makes a miscounted call order fail loudly rather than silently), and expect(events).toEqual(["timeout"]) guards against the scripted-clock trick where a far-future now could make stealIfStale steal a fresh lock — your vanishing-holder script closes that by unlinking before the steal probe. Honest bounds section appreciated.

Informational, not blocking

  • mock.module("node:fs/promises", …) is process-wide in Bun and never uninstalled — every test file that runs after this one in the same bun test process uses your wrapper for all fs access. It passes through for unscripted paths (and I verified the pre-mock real capture makes it non-circular), but it is permanent ambient state; worth a one-line comment saying so, since cross-file leakage is exactly how mystery flakes are born.
  • test-windows failed once on your branch (the known supersede racing appends torn-read flake — the same test #113 hit, and the one #70/#110 documents as baseline-red family on Windows); the rerun passed 491/0. I consider that the documented suite flake, not this change.

@WhollyMindless

Copy link
Copy Markdown
Contributor Author

Added the one-line comment you suggested (f51d53a) - the mock block now says out loud that mock.module is process-wide, never uninstalled, and the wrappers are permanent ambient state for later files. Cross-file leakage being the mystery-flake factory is now stated where the next person can trip over it.

@abogartz abogartz left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Re-verified at f51d53a — APPROVED.

The commit is exactly the one thing the review asked for: a comment block at the mock site (test/lock-absent-holder.test.ts:20-23) stating that mock.module is process-wide, never uninstalled, and that the pass-through wrappers are permanent ambient state for every file that runs after. Nothing else changed (git diff b4f64b9..f51d53a: 4 added lines, one file).

Re-runs: tsc clean, lock-absent-holder.test.ts + lock.test.ts green (13/0/1 skip). The functional review at b4f64b9 (deadline rethrow = never-saw-the-file, everSawHolder gated on stat success, red legs reproduced both ways) stands; this closes the documentation note. Windows CI note also accepted — the torn-read family is the known baseline-red per #110's own docs, and the rerun passed.

@abogartz

Copy link
Copy Markdown
Owner

Flake root-caused: this failure is #114 inheriting a test that landed with #113, not the lock change. git diff b4f64b9..f51d53a is a comment; the failing test is test/capture-compaction.test.ts "an idle broadcast reaches every live subscription, and pays exactly ONE model call", pulled in by update-branch. It has two independent race legs on slow/busy runners:

Leg 1 — winner indeterminacy (correct code can go red). The harness bus (test/harness.ts:72-75) gives every subscriber its own queue, so BOTH setups receive the broadcast and whichever loop reaches the shared cooldown first pays. But only setup a carries a FRESH response — setup b carries DUP_CONTENT, which the gate collapses (no row, gates.ts collapse path). If b wins, the store legitimately holds 1 row and line 385 goes red on correct code. The comment "seed + ONE row, whichever loop got there first" holds only if either winner appends.

Leg 2 — fixed-sleep completion race. waitFor(calls > 0) unblocks when the mock call is initiated; the remaining chain (parse → runWriteGate incl. locked append → evidence record) is real async fs with nothing bounding it except sleep(150). On a loaded Windows runner the append can legitimately take longer. The compaction twin test does not have this hole — it waits on the evidence line, which is recorded after the gate returns.

Suggested fix (both legs, keeps the one-spend invariant as the red leg):

  1. give both setups distinct FRESH responses (the dedup leg belongs to the gate's own tests, and the cooldown test should not depend on who wins);
  2. await waitFor(() => evidence().some((l) => String(l.ev).startsWith("auto_capture_"))) instead of the naked sleep(150), THEN assert calls === 1, then keep a short sleep-window and re-assert calls === 1 (the no-second-fire check stays).

Filed as #117 (test-integrity). Rerunning this leg is a legitimate green for #114 — the lock code is orthogonal; I'll rerun and merge on green per the standing approval.

@abogartz
abogartz merged commit a72ac20 into abogartz:main Oct 26, 2026
4 checks passed
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.14.5 🎉

The release is available on:

Your semantic-release bot 📦🚀

@WhollyMindless

Copy link
Copy Markdown
Contributor Author

Confirmed closed from this side: #119's merge took the idle-broadcast legs out, and #114's merged history is clean (the lock change was the comment diff exactly as you root-caused). One datapoint for the record: the two-leg anatomy propagated to #118's new capture test by construction - both tests verdict on the evidence line rather than a sleep window (leg 2, same fix you specced here; a control test caught the read-too-early variant while it was being written), and the capture path there is single-setup compaction-driven behind a park seam, so leg 1's winner indeterminacy can't arise. The flake's paper trail runs clean end to end: #114 comment -> #117 -> #119.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

lock: the absent-strike deadline rethrow does not mean "absent this whole time"

2 participants