fix(output): synchronize concurrent writes to the prefixed writer - #2983
Open
ikshantshukla123 wants to merge 1 commit into
Open
fix(output): synchronize concurrent writes to the prefixed writer#2983ikshantshukla123 wants to merge 1 commit into
ikshantshukla123 wants to merge 1 commit into
Conversation
WrapWriter uses the same prefixWriter for both stdout and stderr, and os/exec can write to both streams concurrently. Since bytes.Buffer is not safe for concurrent use, this could corrupt its state and cause a "slice bounds out of range" panic. Add a mutex to prefixWriter and lock access in both Write and Close so only one goroutine uses the buffer at a time. Also add a regression test that writes many lines to stdout and stderr concurrently and verifies every line is correctly prefixed; it fails on the unfixed code and is reliably detected with -race.
trulede
reviewed
Aug 17, 2026
trulede
left a comment
Contributor
There was a problem hiding this comment.
The test might be useful during development but is otherwise useless. Why did you include it again?
Contributor
Author
okay okay apology you already flagged this in #2949 and I missed it. I will limit the test when it runs like I will move the concurrency test into its own file gated with go:build race, so it only executes under race and never runs in plain CI. |
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.
Description #2945
This pr fixes a panic that can happen with
output: prefixedmode. when a task writes to both stdout and stderr at the same time, the same prefixWriter ends up being used from two goroutines (os/exec copies each stream from its own goroutine) the shared bytes.Buffer is not safe for concurrent use, so this could corrupt its state and crash with a "slice bounds out of range" panic when their is multiple stdout and stderr concurrently.I added a mutex to prefixWriter and lock it in both Write and Close so the buffer is only ever touched by one goroutine at a time.I also added a regression test that writes 1000 lines to stdout and 1000 to stderr concurrently, then checks that we get exactly 2000 correctly prefixed lines back. I confirmed it panics without the fix, and passes with it (also clean under -race).
Notes
The mutex approach i used was suggested in the issue comments by @trulede sir;
I've reviewed the code and tests myself and can explain any part of it.
Checklist
request per the AI Usage Policy.
(No AI) of this pull request.
and also I have used strings.Split as i found it is used across the repo in (e.g. task_test.go, watch_test.go etc).and it is a completely standard import for test assertions. output_test.go didn't need it before only because the existing tests there used assert. Equal on exact strings rather than splitting or parsing output.
Ready for further discussions.