]> Cypherpunks repositories - gostls13.git/commitdiff
os/exec: add more caveats to StdoutPipe, StderrPipe
authorRuss Cox <rsc@golang.org>
Fri, 13 Sep 2013 19:43:54 +0000 (15:43 -0400)
committerRuss Cox <rsc@golang.org>
Fri, 13 Sep 2013 19:43:54 +0000 (15:43 -0400)
(StdinPipe was taken care of by CL 13329043.)

Fixes #6008.

R=golang-dev, iant
CC=golang-dev
https://golang.org/cl/13606046

src/pkg/os/exec/exec.go

index 582930f2c42282eaf3c53df4cc68c68b84edc1d3..491cc242bb27a0c150db5f8ea6736312be3ba5d1 100644 (file)
@@ -358,8 +358,10 @@ func (c *Cmd) CombinedOutput() ([]byte, error) {
 
 // StdinPipe returns a pipe that will be connected to the command's
 // standard input when the command starts.
-// If the returned WriteCloser is not closed before Wait is called,
-// Wait will close it.
+// The pipe will be closed automatically after Wait sees the command exit.
+// A caller need only call Close to force the pipe to close sooner.
+// For example, if the command being run will not exit until standard input
+// is closed, the caller must close the pipe.
 func (c *Cmd) StdinPipe() (io.WriteCloser, error) {
        if c.Stdin != nil {
                return nil, errors.New("exec: Stdin already set")
@@ -394,7 +396,12 @@ func (c *closeOnce) Close() error {
 
 // StdoutPipe returns a pipe that will be connected to the command's
 // standard output when the command starts.
-// The pipe will be closed automatically after Wait sees the command exit.
+//
+// Wait will close the pipe after seeing the command exit, so most callers
+// need not close the pipe themselves; however, an implication is that
+// it is 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.
+// See the example for idiomatic usage.
 func (c *Cmd) StdoutPipe() (io.ReadCloser, error) {
        if c.Stdout != nil {
                return nil, errors.New("exec: Stdout already set")
@@ -414,7 +421,12 @@ func (c *Cmd) StdoutPipe() (io.ReadCloser, error) {
 
 // StderrPipe returns a pipe that will be connected to the command's
 // standard error when the command starts.
-// The pipe will be closed automatically after Wait sees the command exit.
+//
+// Wait will close the pipe after seeing the command exit, so most callers
+// need not close the pipe themselves; however, an implication is that
+// it is 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.
+// See the StdoutPipe example for idiomatic usage.
 func (c *Cmd) StderrPipe() (io.ReadCloser, error) {
        if c.Stderr != nil {
                return nil, errors.New("exec: Stderr already set")