fix(work-events): clamp replay range to the stat-measured size#1462
Open
tjkang wants to merge 1 commit into
Open
fix(work-events): clamp replay range to the stat-measured size#1462tjkang wants to merge 1 commit into
tjkang wants to merge 1 commit into
Conversation
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.
PR A — fix(work-events): clamp replay range to the stat-measured size
Files:
install/hooks/lib/work-events.ts,install/hooks/tests/work-events-stat-race.test.tsPatch:
patch-1-stat-race.diffThe bug
replayOntoSnapshot()measures the event-log size withstatSync(), then readsthe whole file and replays events from
offsetto the end of the buffer(
buf.subarray(offset)), while returning the stat-measuredsizeas the newoffset:
Appends to the log happen outside the events lock, so the file can grow
between the
stat()and thereadFileSync(). When it does, an event written inthat window is replayed now but not covered by the returned offset. On the
next replay the same event is folded a second time — and if an offset-advance was
emitted in between, the double-apply can roll a field back to a stale value.
The fix (one line)
Clamp the replayed slice to the measured size so the replayed range and the
returned offset always agree:
Events in
[size, buf.length)sit after the returned offset, so the nextread/fold picks them up exactly once. No API change.
Test
Adds a deterministic reproduction that mocks only
statSyncto simulate "thefile grew right after the stat":
phase=starting),phase=execute, offset consistent).Risk
Minimal — the read window only changes in the race case, where it becomes
correct. Outside the race,
buf.length === size, so behavior is unchanged.