1
0
mirror of https://github.com/golang/go synced 2024-11-18 14:04:45 -07:00

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 <bcmills@google.com>
This commit is contained in:
Ian Lance Taylor 2019-09-11 17:57:58 -07:00
parent 531f1d50cc
commit 4ae25ff140

View 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
}
}