From: Ian Lance Taylor Date: Thu, 12 Sep 2019 00:57:58 +0000 (-0700) Subject: os/signal: split up sleeps waiting for signal X-Git-Tag: go1.14beta1~1090 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=4ae25ff1405f9d6b25f40141f42196e8f142f207;p=gostls13.git os/signal: split up sleeps waiting for signal Try to deflake TestNohup. The kernel will deliver a signal as a thread returns from a syscall. If the only active thread is sleeping, and the system is busy, the kernel may not get around to waking up a thread to catch the signal. Try splitting up the sleep, to give the kernel another change to deliver. I don't know if this will help, but it seems worth a try. Fixes #33174 Change-Id: I34b3240af706501ab8538cb25c4846d1d30d7691 Reviewed-on: https://go-review.googlesource.com/c/go/+/194879 Reviewed-by: Bryan C. Mills --- diff --git a/src/os/signal/signal_test.go b/src/os/signal/signal_test.go index 6ea59f4697..d50e595d84 100644 --- a/src/os/signal/signal_test.go +++ b/src/os/signal/signal_test.go @@ -23,14 +23,20 @@ import ( ) func waitSig(t *testing.T, c <-chan os.Signal, sig os.Signal) { - select { - case s := <-c: - if s != sig { - t.Fatalf("signal was %v, want %v", s, sig) + // Sleep multiple times to give the kernel more tries to + // deliver the signal. + for i := 0; i < 10; i++ { + select { + case s := <-c: + if s != sig { + t.Fatalf("signal was %v, want %v", s, sig) + } + return + + case <-time.After(100 * time.Millisecond): } - case <-time.After(1 * time.Second): - t.Fatalf("timeout waiting for %v", sig) } + t.Fatalf("timeout waiting for %v", sig) } // Test that basic signal handling works. @@ -268,7 +274,15 @@ func TestStop(t *testing.T) { if sig == syscall.SIGWINCH || (sig == syscall.SIGHUP && *sendUncaughtSighup == 1) { syscall.Kill(syscall.Getpid(), sig) } - time.Sleep(100 * time.Millisecond) + + // The kernel will deliver a signal as a thread returns + // from a syscall. If the only active thread is sleeping, + // and the system is busy, the kernel may not get around + // to waking up a thread to catch the signal. + // We try splitting up the sleep to give the kernel + // another chance to deliver the signal. + time.Sleep(50 * time.Millisecond) + time.Sleep(50 * time.Millisecond) // Ask for signal c := make(chan os.Signal, 1) @@ -280,10 +294,11 @@ func TestStop(t *testing.T) { waitSig(t, c, sig) Stop(c) + time.Sleep(50 * time.Millisecond) select { case s := <-c: t.Fatalf("unexpected signal %v", s) - case <-time.After(100 * time.Millisecond): + case <-time.After(50 * time.Millisecond): // nothing to read - good } @@ -294,10 +309,11 @@ func TestStop(t *testing.T) { syscall.Kill(syscall.Getpid(), sig) } + time.Sleep(50 * time.Millisecond) select { case s := <-c: t.Fatalf("unexpected signal %v", s) - case <-time.After(100 * time.Millisecond): + case <-time.After(50 * time.Millisecond): // nothing to read - good } }