Skip to content

Commit c8313d4

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
cmd/go: deflake TestScript/test2json_interrupt
- Start handling signals in 'go test' just before starting the test subprocess instead of just after. (It is unlikely that starting the process will cause cmd/go to hang in a way that requires signals to debug, and it is possible that something the test does — such as sending os.Interrupt to its parent processes — will immediately send a signal that needs to be handled.) - In the test-test, don't try to re-parse the parent PIDs after sending signals, and sleep for a much shorter time interval. (Overrunning the sleep caused the next call to strconv.Atoi — which shouldn't even happen! — to fail with a parse error, leading to the failure mode observed in https://build.golang.org/log/f0982dcfc6a362f9c737eec3e7072dcc7ef29e32.) Fixes #56083. Updates #53563. Change-Id: I346a95bdda5619632659ea854f98a9e17a6aede7 Reviewed-on: https://go-review.googlesource.com/c/go/+/456115 TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Bryan Mills <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Bryan Mills <[email protected]>
1 parent b9747e0 commit c8313d4

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

src/cmd/go/internal/test/test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,7 @@ func (r *runTestActor) Act(b *work.Builder, ctx context.Context, a *work.Action)
13061306
cmd.Env = env
13071307
}
13081308

1309+
base.StartSigHandlers()
13091310
t0 := time.Now()
13101311
err = cmd.Start()
13111312

@@ -1314,7 +1315,6 @@ func (r *runTestActor) Act(b *work.Builder, ctx context.Context, a *work.Action)
13141315
// running.
13151316
if err == nil {
13161317
tick := time.NewTimer(testKillTimeout)
1317-
base.StartSigHandlers()
13181318
done := make(chan error)
13191319
go func() {
13201320
done <- cmd.Wait()

src/cmd/go/testdata/script/test2json_interrupt.txt

+16-13
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ stdout -count=1 '"Action":"pass","Package":"example","Test":"FuzzInterrupt"'
77
stdout -count=1 '"Action":"pass","Package":"example","Elapsed":'
88

99
mkdir $WORK/fuzzcache
10-
go test -c . -fuzz=. -o test2json_interrupt_obj
11-
? go tool test2json -p example -t ./test2json_interrupt_obj -test.v -test.paniconexit0 -test.fuzzcachedir $WORK/fuzzcache -test.fuzz FuzzInterrupt -test.run '^$' -test.parallel 1
10+
go test -c . -fuzz=. -o example_test.exe
11+
? go tool test2json -p example -t ./example_test.exe -test.v -test.paniconexit0 -test.fuzzcachedir $WORK/fuzzcache -test.fuzz FuzzInterrupt -test.run '^$' -test.parallel 1
1212
stdout -count=1 '"Action":"pass","Package":"example","Test":"FuzzInterrupt"'
1313
stdout -count=1 '"Action":"pass","Package":"example","Elapsed":'
1414

@@ -37,19 +37,22 @@ func FuzzInterrupt(f *testing.F) {
3737
os.Setenv("GO_TEST_INTERRUPT_PIDS", fmt.Sprintf("%d,%d", ppid, pid))
3838
}
3939

40+
sentInterrupt := false
4041
f.Fuzz(func(t *testing.T, orig string) {
41-
// Simulate a ctrl-C on the keyboard by sending SIGINT
42-
// to the main test process and its parent.
43-
for _, pid := range strings.Split(pids, ",") {
44-
i, err := strconv.Atoi(pid)
45-
if err != nil {
46-
t.Fatal(err)
47-
}
48-
if p, err := os.FindProcess(i); err == nil {
49-
p.Signal(os.Interrupt)
50-
time.Sleep(10 * time.Millisecond)
51-
pids = "" // Only interrupt once.
42+
if !sentInterrupt {
43+
// Simulate a ctrl-C on the keyboard by sending SIGINT
44+
// to the main test process and its parent.
45+
for _, pid := range strings.Split(pids, ",") {
46+
i, err := strconv.Atoi(pid)
47+
if err != nil {
48+
t.Fatal(err)
49+
}
50+
if p, err := os.FindProcess(i); err == nil {
51+
p.Signal(os.Interrupt)
52+
sentInterrupt = true // Only send interrupts once.
53+
}
5254
}
5355
}
56+
time.Sleep(1 * time.Millisecond) // Delay the fuzzer a bit to avoid wasting CPU.
5457
})
5558
}

0 commit comments

Comments
 (0)