Skip to content

Commit

Permalink
os/exec: exit cmd.Run() when context becomes done
Browse files Browse the repository at this point in the history
  • Loading branch information
sxllwx committed Oct 13, 2022
1 parent 6f445a9 commit 1d66847
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/os/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,9 +467,23 @@ func (c *Cmd) writerDescriptor(w io.Writer) (*os.File, error) {
c.childIOFiles = append(c.childIOFiles, pw)
c.parentIOPipes = append(c.parentIOPipes, pr)
c.goroutine = append(c.goroutine, func() error {
_, err := io.Copy(w, pr)
pr.Close() // in case io.Copy stopped due to write error
return err
copyErr := make(chan error)
go func() {
_, err := io.Copy(w, pr)
pr.Close() // in case io.Copy stopped due to write error
copyErr <- err
}()
var ctxDone <-chan struct{}
if c.ctx != nil {
ctxDone = c.ctx.Done()
}
select {
case err := <-copyErr:
return err
case <-ctxDone:
//pr.Close()
return c.ctx.Err()
}
})
return pw, nil
}
Expand Down

0 comments on commit 1d66847

Please sign in to comment.