]> Cypherpunks repositories - gostls13.git/commitdiff
os/exec: recombine goroutinePipes and userPipes
authorBryan C. Mills <bcmills@google.com>
Fri, 30 Sep 2022 16:46:40 +0000 (12:46 -0400)
committerGopher Robot <gobot@golang.org>
Sat, 1 Oct 2022 00:12:32 +0000 (00:12 +0000)
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 <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>

src/os/exec/exec.go

index 074c429657f4d22e1194f349c1f1eb1d4b05dd7d..8e6f709a2f13b5fe086039f686fba7353c6495fa 100644 (file)
@@ -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
 }