Commit 4ae25ff1 authored by Ian Lance Taylor's avatar Ian Lance Taylor

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/+/194879Reviewed-by: default avatarBryan C. Mills <bcmills@google.com>
parent 531f1d50
......@@ -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
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment