aboutsummaryrefslogtreecommitdiff
path: root/src/os/exec/exec_test.go
diff options
context:
space:
mode:
authorAlan Donovan <adonovan@google.com>2025-12-09 10:06:23 -0500
committerAlan Donovan <adonovan@google.com>2025-12-22 10:43:07 -0800
commit3d77a0b15ea2a6d2f7b3e2ba483f148d7c6ee174 (patch)
tree171d863efefb26bcb6d1846977669b6edf42748d /src/os/exec/exec_test.go
parent7ecb1f36acab7b48d77991d58d456a34074a2d0e (diff)
downloadgo-3d77a0b15ea2a6d2f7b3e2ba483f148d7c6ee174.tar.xz
os/exec: second call to Cmd.Start is always an error
Previously it would return an error only if the first call resulted in process creation, contra the intent of the comment at exec.Cmd: // A Cmd cannot be reused after calling its [Cmd.Start], [Cmd.Run], // [Cmd.Output], or [Cmd.CombinedOutput] methods. Also, clear the Cmd.goroutines slice in case of failure to start a process, so that the closures can be GC'd and their pipe fds finalized and closed. Fixes #76746 Change-Id: Ic63a4dced0aa52c2d4be7d44f6dcfc84ee22282c Reviewed-on: https://go-review.googlesource.com/c/go/+/728642 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Damien Neil <dneil@google.com>
Diffstat (limited to 'src/os/exec/exec_test.go')
-rw-r--r--src/os/exec/exec_test.go26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/os/exec/exec_test.go b/src/os/exec/exec_test.go
index 1decebdc22..bf2f3da535 100644
--- a/src/os/exec/exec_test.go
+++ b/src/os/exec/exec_test.go
@@ -1839,3 +1839,29 @@ func TestAbsPathExec(t *testing.T) {
}
})
}
+
+// Calling Start twice is an error, regardless of outcome.
+func TestStart_twice(t *testing.T) {
+ testenv.MustHaveExec(t)
+
+ cmd := exec.Command("/bin/nonesuch")
+ for i, want := range []string{
+ cond(runtime.GOOS == "windows",
+ `exec: "/bin/nonesuch": executable file not found in %PATH%`,
+ "fork/exec /bin/nonesuch: no such file or directory"),
+ "exec: already started",
+ } {
+ err := cmd.Start()
+ if got := fmt.Sprint(err); got != want {
+ t.Errorf("Start call #%d return err %q, want %q", i+1, got, want)
+ }
+ }
+}
+
+func cond[T any](cond bool, t, f T) T {
+ if cond {
+ return t
+ } else {
+ return f
+ }
+}