fix(tracking): close append/compaction TOCTOU lost-write - #92
Merged
Conversation
…pend lock) The journal append path used a bare appendFileSync (O_APPEND) with no mutual exclusion against the compactor. Compaction is rename -> fold -> unlink on a fresh inode; an append could straddle open->write between compaction's rename and unlink, landing its write in the renamed inode that then got unlinked -- a silent lost write, never surfaced by the per-line HMAC (fail-closed per line has nothing to reject: the write simply never reached the surviving file). Fix: appendEvent now serialises on withTrackLockSyncBlocking, the same track.lock the compaction takes, spinning (1ms step, stale-lock TTL as the only anti-deadlock guard) until acquired -- never skipped, unlike the existing non-blocking withTrackLockSync used elsewhere. An append can no longer race compaction's rename/fold/unlink window. Probe (test/track-journal-toctou.test.ts) is deterministic and proven non-vacant independently: (A) a child process blocked on a manually held track.lock must NOT complete its append until the lock is released -- red without the fix (bare appendFileSync ignores the lock), green with it. (B) a real appendEvent loop running during a real maybeCompactJournal must serialise behind the compaction with zero event loss -- red without the fix (loop finishes inside the compaction window), green with it. Verified non-flaky 5/5, full suite 900/0.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
appendEventnow serialises on a blocking twin of the existing track lock (withTrackLockSyncBlocking), never skipped, so an append can no longer race compaction's rename/unlink window.test/track-journal-toctou.test.ts): a manual-lock block test and a real append-loop-vs-real-compaction race test asserting zero event loss.package.jsonto 0.1.83 (PATCH) and updatesCHANGELOG.md.Test plan
bunx tsc --noEmitcleanbun test— 900 pass / 1 skip / 0 failgrep -rEn "bun:|Bun\.|from ['"'"'bun['"'"']" src/tracking— no runtime hits20f72154e8876e02180ba7f8dd8a018005968cb5)