]> Cypherpunks repositories - gostls13.git/commitdiff
os/signal: split up sleeps waiting for signal
authorIan Lance Taylor <iant@golang.org>
Thu, 12 Sep 2019 00:57:58 +0000 (17:57 -0700)
committerIan Lance Taylor <iant@golang.org>
Mon, 16 Sep 2019 15:19:42 +0000 (15:19 +0000)
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 <bcmills@google.com>
src/os/signal/signal_test.go

index 6ea59f4697f622a4cf0c2a1802fd450a103d4d93..d50e595d84d826a921af71ef3470de965e1395c0 100644 (file)
@@ -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
                }
        }