]> Cypherpunks repositories - gostls13.git/commitdiff
os/exec: close read pipe if copy to io.Writer fails
authorRuss Cox <rsc@golang.org>
Wed, 22 Jul 2015 20:50:00 +0000 (16:50 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 22 Jul 2015 21:37:18 +0000 (21:37 +0000)
Fixes #10400.

Change-Id: Ic486cb8af4c40660fd1a2e3d10986975acba3f19
Reviewed-on: https://go-review.googlesource.com/12537
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/os/exec/exec.go
src/os/exec/exec_test.go

index fcc37870ede5f1edfece4f90a40af4d5694a97c8..8a84e263dc8bf013e424dbbade63b72fd5946633 100644 (file)
@@ -230,6 +230,7 @@ func (c *Cmd) writerDescriptor(w io.Writer) (f *os.File, err error) {
        c.closeAfterWait = append(c.closeAfterWait, 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
        })
        return pw, nil
index 6888d29cd8f5005028cb9237e4253f6f9ac9df41..28be21ce63dc47c2d4e063f57529a4238a75dad9 100644 (file)
@@ -786,3 +786,33 @@ func TestIgnorePipeErrorOnSuccess(t *testing.T) {
                t.Errorf("output = %q; want %q", got, want)
        }
 }
+
+type badWriter struct{}
+
+func (w *badWriter) Write(data []byte) (int, error) {
+       return 0, io.ErrUnexpectedEOF
+}
+
+func TestClosePipeOnCopyError(t *testing.T) {
+       testenv.MustHaveExec(t)
+
+       if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
+               t.Skipf("skipping test on %s - no yes command", runtime.GOOS)
+       }
+       cmd := exec.Command("yes")
+       cmd.Stdout = new(badWriter)
+       c := make(chan int, 1)
+       go func() {
+               err := cmd.Run()
+               if err == nil {
+                       t.Errorf("yes completed successfully")
+               }
+               c <- 1
+       }()
+       select {
+       case <-c:
+               // ok
+       case <-time.After(5 * time.Second):
+               t.Fatalf("yes got stuck writing to bad writer")
+       }
+}