fix(lock): atomic heartbeat rewrite ends the #904 torn-read flake - #1196
Merged
Conversation
…ncating in place The heartbeat rewrote session-refresh.lock with a plain writeFile, which truncates before it writes, so any reader could observe an empty or partial body for the width of the write. observe() tolerated that window with retries, but every plain reader (the lock tests included) raced it, which is the JSON parse flake behind #904. The rewrite now lands next to the lock and renames over it, so a reader sees the previous body or the next one, never a torn one. A refused replace on Windows skips the tick, which staleness absorbs at nine heartbeats per staleMs. observe() keeps its tolerance because older codeburn versions sharing the cache dir still rewrite in place. The new regression test hammers plain reads against a 1ms heartbeat: it failed 5 of 5 runs against the old rewrite and passes 10 of 10 serial lock-suite runs with this change.
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.
The cache-refresh lock heartbeat rewrote the lock file in place (truncate then write), exposing every concurrent reader to an empty or partial JSON body.
observe()papers over the window with stat-bracketed retries, but plain readers race it — that is theUnexpected end of JSON inputflake that hit main and two PR runs today (#904 family).Fix: the heartbeat writes the next body beside the lock and
rename()s over it. POSIX rename is atomic and Node maps it to a replacing move on Windows, so a reader sees the old body or the new body, never a torn one. A refused replace (Windows busy handle) skips the tick and cleans its temp file; staleness is nine heartbeats wide, so a skipped tick is absorbed.observe()keeps its tolerance for the exclusive-create window and for older versions that still rewrite in place against the same cache dir.Evidence:
npm run test:locks): 10/10 consecutive green runs (36 tests each).tscandnpm run buildclean.Remaining #904 family member (the takeover-window
completed-by-othermisclassification in the process test) is a different race and stays tracked there.