aboutsummaryrefslogtreecommitdiff
path: root/src/os/exec/exec_test.go
diff options
context:
space:
mode:
authorBryan C. Mills <bcmills@google.com>2022-09-28 13:29:12 -0400
committerGopher Robot <gobot@golang.org>2022-09-29 02:28:26 +0000
commit4d6ca68a85e42c75683bc96aa540207566f54e26 (patch)
tree603cece2f0ab25bbfdd5e690ecf5947dbd41b985 /src/os/exec/exec_test.go
parent13d48bb6a1ad3787c10fbbe4265ad11672d90a37 (diff)
downloadgo-4d6ca68a85e42c75683bc96aa540207566f54e26.tar.xz
os/exec: do not close pipes on a double-Start error
This fixes a bug introduced in CL 401834 in which calling Start twice with pipes attached to a command would spuriously close those pipes when returning the error from the second Start call. For #50436. Change-Id: I3563cc99c0a0987752190fef95da3e9927a76fda Reviewed-on: https://go-review.googlesource.com/c/go/+/436095 Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Bryan Mills <bcmills@google.com> Auto-Submit: Bryan Mills <bcmills@google.com>
Diffstat (limited to 'src/os/exec/exec_test.go')
-rw-r--r--src/os/exec/exec_test.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/os/exec/exec_test.go b/src/os/exec/exec_test.go
index 52001bf9e3..07ac0cf3d4 100644
--- a/src/os/exec/exec_test.go
+++ b/src/os/exec/exec_test.go
@@ -1094,3 +1094,46 @@ func TestNoPath(t *testing.T) {
t.Errorf("new(Cmd).Start() = %v, want %q", err, want)
}
}
+
+// TestDoubleStartLeavesPipesOpen checks for a regression in which calling
+// Start twice, which returns an error on the second call, would spuriously
+// close the pipes established in the first call.
+func TestDoubleStartLeavesPipesOpen(t *testing.T) {
+ cmd := helperCommand(t, "pipetest")
+ in, err := cmd.StdinPipe()
+ if err != nil {
+ t.Fatal(err)
+ }
+ out, err := cmd.StdoutPipe()
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := cmd.Start(); err != nil {
+ t.Fatal(err)
+ }
+ if err := cmd.Start(); err == nil || !strings.HasSuffix(err.Error(), "already started") {
+ t.Fatalf("second call to Start returned a nil; want an 'already started' error")
+ }
+
+ outc := make(chan []byte, 1)
+ go func() {
+ b, err := io.ReadAll(out)
+ if err != nil {
+ t.Error(err)
+ }
+ outc <- b
+ }()
+
+ const msg = "O:Hello, pipe!\n"
+
+ _, err = io.WriteString(in, msg)
+ if err != nil {
+ t.Fatal(err)
+ }
+ in.Close()
+
+ b := <-outc
+ if !bytes.Equal(b, []byte(msg)) {
+ t.Fatalf("read %q from stdout pipe; want %q", b, msg)
+ }
+}