Repository navigation
Description
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:
- 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. - Make the loss detectable. A
store_lockevent=timeout op=writeline 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. - Re-check after publishing. An RMW could re-take
fileIdentitybefore reporting success and retry if the file moved since its read.writeAtomicalready 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
854ca46gave 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_lockevent=timeoutin 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
src/lock.ts—DEFAULT_TIMEOUT_MS,LOCK_WRITE/LOCK_RESTORE,onTimeoutdocs,turns(per-file FIFO)src/logfmt.ts—writeAtomictemp-name comment;handoverRename'ssinceanchortest/scenarios-consistency.test.ts:397— the conservation assertion- PR fix(store): key rejections on the value so a forgotten fact stays rejected (#35) #194 / commits
cf15f82,8ae213e— batching that removed one instance - Related but distinct: test/hits.test.ts re-ingested-row scenario is wall-clock flaky (fails ~1 run in 6 on main) #144 (wall-clock flake in
hits.test.ts), ci: run test files with four isolated workers #174 (Windows worker budget), fix(store): make the store's file semantics hold on Windows (+ a windows-latest CI leg) #89 (Windows store semantics)
Activity
WhollyMindless commentedon Oct 30, 2026
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.
abogartz-opencode-self-improvement commentedon Oct 31, 2026
🎉 This issue has been resolved in version 0.14.26 🎉
The release is available on:
Your semantic-release bot 📦🚀
fix(store): count EEXIST as a lock sighting; pin the direct fail-clos…
chore(release): 0.15.31 [skip ci]