Skip to content

Whole-file publishers proceed UNLOCKED past the 5s lock budget — documented as never losing a write, silently destroys rows #196

Closed

Description

@abogartz

Severity: high

Silent row loss under write contention, caused by a safety invariant that is documented in src/lock.ts but does not hold. Secondary effect is a ~15% Windows CI failure rate that gets attributed to whichever PR happened to be running.

Not critical: it needs >5 s of lock starvation (large day file + concurrent sessions + slow I/O), and prune() snapshots before publishing, so lost rows are usually recoverable with memory_undo. It is high because the loss is unreported, and because the comment asserting safety is false — the next person to touch this will trust it, as I did.

The falsified claim

LOCK_WRITE sets no onTimeout, so it takes the default branch: run the mutation anyway, unlocked.

// src/lock.ts
const DEFAULT_TIMEOUT_MS = 5_000;

export const LOCK_WRITE: LockOptions = {
  // no onTimeout → "proceed"
  onEvent: (event, waitedMs) => { void record("store_lock", { event, waitedMs, op: "write" }); },
};

The doc on that option says proceeding is "strictly no worse than the pre-lock behaviour, and it never costs a user their write."

The first half is defensible. The second half is false, and src/logfmt.ts already knows it — writeAtomic's comment explains that the temp name is per-call precisely because "a starved waiter's budget expires and lock.ts lets it proceed anyway … That degraded writer can be inside writeAtomic for the same file at the same instant."

So: proceed is safe for two appenders. It is not safe for an appender and a whole-file read-modify-write. The RMW publishes the snapshot it read; an append that lands inside that window is overwritten. The writer that timed out believes its row is stored — it got no error — and the row is gone.

Six publishers take LOCK_WRITE and are therefore exposed: prune (memory.ts:653), batch supersede (memory.ts:466), single supersede (memory.ts:393), viewer delete (viewer/mutate.ts:138), blocklist rewrite (blocklist.ts:73), and the tombstone rewrite (tombstones.ts:316). snapshot restore (undo.ts:334) is the one path that already fails closed — which proves the mitigation is a deliberate choice the codebase makes where someone thought about it, not a new idea.

Observed, not theorised

prune() on a large day file, two child processes appending, Windows runner. Test asserts conservation and finds 4704 of 5700 untargeted rows vanished (test/scenarios-consistency.test.ts:397).

run workload windows result
41112466339 a1 prune, per-value lock cycle fail prune scenario 4704 rows lost + viewer scenario
41112466339 a2 same commit fail prune scenario again
41115615447 a1 batched to one lock cycle pass —
41115615447 a2 same commit fail two unrelated auto-capture tests
41115615447 a3 same commit pass —

PR #194 removed its own contribution (one store-lock create/unlink pair per deleted value → one per memory_forget). That fixed the reproducible failure. It did not remove the mechanism: any write path that holds an RMW longer than ~5 s while others wait still destroys their rows, silently.

The varying failures in the last three rows are the same underlying sensitivity showing up as test flake, on a runner where the repo is already fighting it (ci/parallel-tests, 854ca46 budgeting the import scenario, #190 instrumenting ACL phase timings).

Why it is worth fixing rather than budgeting

Timeouts cannot fix this. Raising a test timeout or the lock budget only narrows the window; the failure mode stays "no error, row gone." Three directions, roughly in order of value:

  1. Fail closed on RMW. Give the whole-file publishers (prune, viewer delete, supersede) onTimeout: "fail" like restore already has. A refused prune retries; a lost row does not come back on its own. This is a small diff and removes the class.
  2. Make the loss detectable. A store_lock event=timeout op=write line is already recorded — but nothing connects it to the write that then proceeded, and nothing asserts on it. Thread an id through so a timeout is attributable, then either surface it in the tool reply or refuse.
  3. Re-check after publishing. An RMW could re-take fileIdentity before reporting success and retry if the file moved since its read. writeAtomic already does this on the rename-retry path (since); the unguarded case is the starved writer whose rename succeeds first.

Secondary: CI velocity

On 2026-10-30 test-windows failed on ~15% of runs across three unrelated branches (fix/gate-evidence, fix/gate-reader, and this one), each time a different test in the heavy cross-process scenarios — several at [5015ms]/[5046ms], i.e. right on Bun's default 5 s while the scenario's own budget comment already concedes "Bun's default 5s deadline can abort valid work on a busy Windows runner with four test workers (#174)".

Consequence worth naming: I initially assumed this flake was pre-existing ambient noise, and it partly was — the cost of that assumption would have been a shipped data-loss bug. When "known flaky" and "genuine defect" share a signature, effort gets misrouted in whichever direction the label points.

Two cheap improvements, independent of the lock work:

  • Give the conservation scenarios an explicit budget like 854ca46 gave the import scenario, and assert their row counts rather than leaving the 5 s default as the de-facto assertion.
  • Have CI fail loudly on any store_lock event=timeout in a test's evidence output. If a run starved a writer, that is the interesting fact — far more than which assertion tripped at 5015 ms.

References

Activity

  1. added
    severity:high
    bug
    on Oct 30, 2026
  2. WhollyMindless commented on Oct 30, 2026

    @WhollyMindless
    Contributor

    Claiming this: no existing assignee, claim comment, or matching open PR is visible. I will first pin the timeout/data-loss interleave deterministically, then make the shared write policy refuse unsafe unlocked mutations and check the tool/viewer failure paths.

    One detail I will cover explicitly: changing only whole-file publishers is insufficient if an appender can still time out and enter while a publisher holds the lock. Both sides of that shared-file protocol need to respect exclusion. I will keep this separate from your #194 and coordinate any overlap before landing. Please redirect if you have already started it elsewhere.

  3. abogartz-opencode-self-improvement commented on Oct 31, 2026

    @abogartz-opencode-self-improvement

    🎉 This issue has been resolved in version 0.14.26 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugreleasedseverity:high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions