Uh oh!
There was an error while loading. Please reload this page.
De-flake atomicWriteFile CONCURRENT_EDIT mtime test - #273
Conversation
The stale-mtime assertion relied on the preceding atomicWriteFile('v2')
landing in a later filesystem mtime tick than the caller's read. On a
fast runner both writes share one mtime tick, so the on-disk mtime still
equals expectedMtimeMs, the guard (current.mtimeMs !== expectedMtimeMs)
never fires, and assert.rejects fails with "Missing expected rejection".
Simulate the concurrent edit deterministically by bumping the on-disk
mtime with fs.utimes to a value strictly distinct from expectedMtimeMs
before the guarded write, so the CONCURRENT_EDIT rejection is asserted
independent of filesystem mtime granularity. Production atomicWriteFile
behavior is unchanged.
Fixes#272
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>philcunliffe
commented
Jul 7, 2026
🔍 neutral review — approveFocused review, scaled to a 2-line, test-only change. The diff is exactly a guaranteed-distinct mtime bump ( Evidence it works: CI is green and the previously-flaky Verdict: approve. Held for a human to merge — closes #272 via Automated neutral review (focused — scaled to the change's stakes). |
What
De-flakes
atomicWriteFile enforces expectedMtimeMs (CONCURRENT_EDIT)intest/core/util-fs-atomic.test.js, which intermittently failed on fast CI withAssertionError: Missing expected rejection(confirmed non-deterministic on PRs #259 and #266 — same commit failed onetestjob, passed the sibling, cleared on re-run).Why
The stale-mtime assertion relied on the preceding
atomicWriteFile('v2', …)bumping the target's on-disk mtime past the caller's capturedmtimeMs. On a fast runner both writes land within the same filesystem mtime tick, so the on-disk mtime still equalsexpectedMtimeMs, the guard (current.mtimeMs !== expectedMtimeMs) never fires, and noCONCURRENT_EDITrejection happens.Reproduced faithfully by pinning the on-disk mtime to a coarse whole-ms tick and re-pinning it after the write: same-tick → guard silent → no rejection.
Fix
Simulate the concurrent edit deterministically: bump the target's on-disk mtime with
fs.utimesto a value strictly distinct fromexpectedMtimeMs(+1000ms) before the guarded write. TheCONCURRENT_EDITrejection is now asserted independent of filesystem mtime granularity. Test-only change — productionatomicWriteFilebehavior is unchanged.Verification
node --test test/core/util-fs-atomic.test.js× 20: all pass.npm test: 1875 pass, 0 fail, 1 skipped.npm run typecheckandnpm run build:types: clean (exit 0).Note (out of scope)
The production guard has an inherent same-tick blind spot: a concurrent writer that modifies the file within the same mtime tick as the caller's read is undetectable by mtime comparison. This is the standard TOCTOU limitation of lightweight mtime-based optimistic-concurrency guards, not a regression — left unchanged here.
Fixes#272