Skip to content

[skip-changelog] Proper handling of stdout/stderr copy in subprocess #1862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions executils/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,22 @@ func (p *Process) StdinPipe() (io.WriteCloser, error) {

// StdoutPipe returns a pipe that will be connected to the command's standard
// output when the command starts.
//
// Wait will close the pipe after seeing the command exit, so most callers
// don't need to close the pipe themselves. It is thus incorrect to call Wait
// before all reads from the pipe have completed.
// For the same reason, it is incorrect to call Run when using StdoutPipe.
func (p *Process) StdoutPipe() (io.ReadCloser, error) {
return p.cmd.StdoutPipe()
}

// StderrPipe returns a pipe that will be connected to the command's standard
// error when the command starts.
//
// Wait will close the pipe after seeing the command exit, so most callers
// don't need to close the pipe themselves. It is thus incorrect to call Wait
// before all reads from the pipe have completed.
// For the same reason, it is incorrect to use Run when using StderrPipe.
func (p *Process) StderrPipe() (io.ReadCloser, error) {
return p.cmd.StderrPipe()
}
Expand Down
19 changes: 11 additions & 8 deletions internal/integrationtest/arduino-cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,22 @@ func (cli *ArduinoCLI) Run(args ...string) ([]byte, []byte, error) {
cli.t.NoError(cliProc.Start())

var stdoutBuf, stderrBuf bytes.Buffer
stdoutCtx, stdoutCancel := context.WithCancel(context.Background())
stderrCtx, stderrCancel := context.WithCancel(context.Background())
var wg sync.WaitGroup
wg.Add(2)
go func() {
io.Copy(&stdoutBuf, io.TeeReader(stdout, os.Stdout))
stdoutCancel()
defer wg.Done()
if _, err := io.Copy(&stdoutBuf, io.TeeReader(stdout, os.Stdout)); err != nil {
fmt.Println(color.HiBlackString("<<< stdout copy error:"), err)
}
}()
go func() {
io.Copy(&stderrBuf, io.TeeReader(stderr, os.Stderr))
stderrCancel()
defer wg.Done()
if _, err := io.Copy(&stderrBuf, io.TeeReader(stderr, os.Stderr)); err != nil {
fmt.Println(color.HiBlackString("<<< stderr copy error:"), err)
}
}()
wg.Wait()
cliErr := cliProc.Wait()
<-stdoutCtx.Done()
<-stderrCtx.Done()
fmt.Println(color.HiBlackString("<<< Run completed (err = %v)", cliErr))

return stdoutBuf.Bytes(), stderrBuf.Bytes(), cliErr
Expand Down