fix(k8s): data race + premature output read in runCommandStreaming#238
Merged
Conversation
The stdout/stderr scanner goroutines both wrote to one strings.Builder with no synchronization (Builder isn't concurrency-safe), and output was read after cmd.Wait() without joining the goroutines — a data race and a torn/incomplete read. Guard the builder with a mutex and join both scanners via a WaitGroup, draining the pipes before cmd.Wait() per the os/exec contract. Closes #208
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.
Problem
runCommandStreamingspawns separate stdout and stderr scanner goroutines that both calloutput.WriteStringon a singlestrings.Builderwith no synchronization.strings.Builderis not safe for concurrent use → data race. There's also noWaitGroup, andoutput.String()is read right aftercmd.Wait(), which does not join the scanners — so the read can be torn/incomplete.Where
internal/k8s/plan/exec.go:300-337(runCommandStreaming)Fix
strings.Builderwith async.Mutex.sync.WaitGroupand drain the pipes (wg.Wait()) beforecmd.Wait(), per theos/execdocs ("it is incorrect to call Wait before all reads from the pipe have completed").Testing
go build ./internal/k8s/...,go vet, andgo test -race ./internal/k8s/plan/all pass.Closes #208