From c9a62b7e711f5c1f1a73e0c3a5b6a2e5b67033e2 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Fri, 30 Sep 2022 12:46:40 -0400 Subject: [PATCH] os/exec: recombine goroutinePipes and userPipes This change undoes CL 401894, because on further consideration it turns out not to be needed. This also makes (*Cmd).closeDescriptors a free function, since it does not actually use the receiver in any way and is not needed to satisfy any interfaces. For #50436. Change-Id: I7915265b0e6398ed5a34fae0c12873ab08a14194 Reviewed-on: https://go-review.googlesource.com/c/go/+/437175 Run-TryBot: Bryan Mills Reviewed-by: Ian Lance Taylor TryBot-Result: Gopher Robot Auto-Submit: Bryan Mills --- src/os/exec/exec.go | 40 +++++++++++++++------------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/src/os/exec/exec.go b/src/os/exec/exec.go index 074c429657..8e6f709a2f 100644 --- a/src/os/exec/exec.go +++ b/src/os/exec/exec.go @@ -226,17 +226,11 @@ type Cmd struct { // are inherited by the child process. childIOFiles []io.Closer - // goroutinePipes holds closers for the parent's end of any pipes + // parentIOPipes holds closers for the parent's end of any pipes // connected to the child's stdin, stdout, and/or stderr streams - // that are pumped (and ultimately closed) by goroutines controlled by - // the Cmd itself (not supplied by or returned to the caller). - goroutinePipes []io.Closer - - // userPipes holds closers for the parent's end of any pipes - // connected to the child's stdin, stdout and/or stderr streams - // that were opened and returned by the Cmd's Pipe methods. - // These should be closed when Wait completes. - userPipes []io.Closer + // that were opened by the Cmd itself (not supplied by the caller). + // These should be closed after Wait sees the command exit. + parentIOPipes []io.Closer // goroutine holds a set of closures to execute to copy data // to and/or from the command's I/O pipes. @@ -380,7 +374,7 @@ func (c *Cmd) childStdin() (*os.File, error) { } c.childIOFiles = append(c.childIOFiles, pr) - c.goroutinePipes = append(c.goroutinePipes, pw) + c.parentIOPipes = append(c.parentIOPipes, pw) c.goroutine = append(c.goroutine, func() error { _, err := io.Copy(pw, c.Stdin) if skipStdinCopyError(err) { @@ -429,7 +423,7 @@ func (c *Cmd) writerDescriptor(w io.Writer) (*os.File, error) { } c.childIOFiles = append(c.childIOFiles, pw) - c.goroutinePipes = append(c.goroutinePipes, pr) + 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 @@ -438,7 +432,7 @@ func (c *Cmd) writerDescriptor(w io.Writer) (*os.File, error) { return pw, nil } -func (c *Cmd) closeDescriptors(closers []io.Closer) { +func closeDescriptors(closers []io.Closer) { for _, fd := range closers { fd.Close() } @@ -505,14 +499,12 @@ func (c *Cmd) Start() error { started := false defer func() { - c.closeDescriptors(c.childIOFiles) + closeDescriptors(c.childIOFiles) c.childIOFiles = nil if !started { - c.closeDescriptors(c.goroutinePipes) - c.goroutinePipes = nil - c.closeDescriptors(c.userPipes) - c.userPipes = nil + closeDescriptors(c.parentIOPipes) + c.parentIOPipes = nil } }() @@ -709,10 +701,8 @@ func (c *Cmd) Wait() error { err = copyErr } } - c.goroutinePipes = nil // Already closed by their respective goroutines. - - c.closeDescriptors(c.userPipes) - c.userPipes = nil + closeDescriptors(c.parentIOPipes) + c.parentIOPipes = nil return err } @@ -777,7 +767,7 @@ func (c *Cmd) StdinPipe() (io.WriteCloser, error) { c.Stdin = pr c.childIOFiles = append(c.childIOFiles, pr) wc := &closeOnce{File: pw} - c.userPipes = append(c.userPipes, wc) + c.parentIOPipes = append(c.parentIOPipes, wc) return wc, nil } @@ -818,7 +808,7 @@ func (c *Cmd) StdoutPipe() (io.ReadCloser, error) { } c.Stdout = pw c.childIOFiles = append(c.childIOFiles, pw) - c.userPipes = append(c.userPipes, pr) + c.parentIOPipes = append(c.parentIOPipes, pr) return pr, nil } @@ -843,7 +833,7 @@ func (c *Cmd) StderrPipe() (io.ReadCloser, error) { } c.Stderr = pw c.childIOFiles = append(c.childIOFiles, pw) - c.userPipes = append(c.userPipes, pr) + c.parentIOPipes = append(c.parentIOPipes, pr) return pr, nil } -- 2.50.0