fix(agent/agentproc): read process info before output to prevent TOCTOU#25646
Merged
Conversation
handleProcessOutput read proc.output() then proc.info() using separate locks. Between the two reads the exit goroutine could finish I/O and set running=false, pairing stale output with final status. On Windows CI this caused OutputExceedsBuffer to flake when the buffer snapshot caught mid-write data (OmittedBytes=0) but info reported the process as exited. Swap the read order so info is read first. The exit goroutine completes cmd.Wait (draining all pipe data) before setting running=false, so seeing Running=false guarantees the subsequent output read reflects the final buffer state.
9e61962 to
6551f3d
Compare
johnstcn
approved these changes
May 25, 2026
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
handleProcessOutputreadproc.output()thenproc.info()using separate locks (buf.muvsproc.mu). Between the two reads, the exit goroutine could finish I/O and setrunning=false, pairing a stale buffer snapshot with final process status.On Windows CI this caused
TestProcessLifecycle/OutputExceedsBufferto flake: the buffer was captured mid-write (OmittedBytes=0) while info reported the process as exited.Swap the read order:
info()first, thenoutput(). The exit goroutine completescmd.Wait()(draining all pipe data) before settingrunning=false, soRunning=falseguarantees the subsequent output read reflects the final buffer state.Proof: dlv-controlled TOCTOU reproduction
Used dlv to freeze execution between the two reads and mutate process state, proving the race under controlled conditions.
Buggy order (output-first, breakpoint between reads):
Fixed order (info-first, breakpoint between reads):
Closes https://linear.app/codercom/issue/CODAGT-399