Skip to content

Commit 00dbcb3

Browse files
author
Bryan C. Mills
committed
os/exec: in TestContextCancel, dump goroutines on failure
If this test fails, we want to know exactly what the os/exec goroutines are doing. Panicking gives us a goroutine dump, whereas t.Fatal does not. While we're here, use exponential backoff instead of a hard-coded 1ms sleep. We want to give the OS enough time to actually terminate the subprocess. For #42061 Change-Id: I3d50a71ac314853c68a935218e7f97ce18b08b5b Reviewed-on: https://go-review.googlesource.com/c/go/+/368317 Trust: Bryan C. Mills <bcmills@google.com> Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
1 parent c3a7fb2 commit 00dbcb3

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/os/exec/exec_test.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,10 @@ func TestContext(t *testing.T) {
954954
}
955955

956956
func TestContextCancel(t *testing.T) {
957+
// To reduce noise in the final goroutine dump,
958+
// let other parallel tests complete if possible.
959+
t.Parallel()
960+
957961
ctx, cancel := context.WithCancel(context.Background())
958962
defer cancel()
959963
c := helperCommandContext(t, ctx, "cat")
@@ -978,14 +982,25 @@ func TestContextCancel(t *testing.T) {
978982
// Calling cancel should have killed the process, so writes
979983
// should now fail. Give the process a little while to die.
980984
start := time.Now()
985+
delay := 1 * time.Millisecond
981986
for {
982987
if _, err := io.WriteString(stdin, "echo"); err != nil {
983988
break
984989
}
990+
985991
if time.Since(start) > time.Minute {
986-
t.Fatal("canceling context did not stop program")
992+
// Panic instead of calling t.Fatal so that we get a goroutine dump.
993+
// We want to know exactly what the os/exec goroutines got stuck on.
994+
panic("canceling context did not stop program")
995+
}
996+
997+
// Back off exponentially (up to 1-second sleeps) to give the OS time to
998+
// terminate the process.
999+
delay *= 2
1000+
if delay > 1*time.Second {
1001+
delay = 1 * time.Second
9871002
}
988-
time.Sleep(time.Millisecond)
1003+
time.Sleep(delay)
9891004
}
9901005

9911006
if err := c.Wait(); err == nil {

0 commit comments

Comments
 (0)