Skip to content

Commit 340a2c5

Browse files
committed
Add test
1 parent f2cadb6 commit 340a2c5

File tree

1 file changed

+59
-3
lines changed

1 file changed

+59
-3
lines changed

src/os/signal/signal_test.go

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,7 @@ func testCancel(t *testing.T, ignore bool) {
217217
// Either way, this should undo both calls to Notify above.
218218
if ignore {
219219
Ignore(syscall.SIGWINCH, syscall.SIGHUP)
220-
// Don't bother deferring a call to Reset: it is documented to undo Notify,
221-
// but its documentation says nothing about Ignore, and (as of the time of
222-
// writing) it empirically does not undo an Ignore.
220+
defer Reset(syscall.SIGWINCH, syscall.SIGHUP)
223221
} else {
224222
Reset(syscall.SIGWINCH, syscall.SIGHUP)
225223
}
@@ -913,3 +911,61 @@ func TestSignalTrace(t *testing.T) {
913911
close(quit)
914912
<-done
915913
}
914+
915+
// #46321 test Reset actually undoes the effect of Ignore.
916+
func TestResetIgnore(t *testing.T) {
917+
if os.Getenv("GO_TEST_RESET_IGNORE") != "" {
918+
s, err := strconv.Atoi(os.Getenv("GO_TEST_RESET_IGNORE"))
919+
if err != nil {
920+
t.Fatalf("failed to parse signal: %v", err)
921+
}
922+
resetIgnoreTestProgram(t, syscall.Signal(s))
923+
}
924+
925+
sigs := []syscall.Signal{
926+
syscall.SIGINT,
927+
syscall.SIGHUP,
928+
syscall.SIGUSR1,
929+
syscall.SIGTERM,
930+
syscall.SIGCHLD,
931+
syscall.SIGWINCH,
932+
}
933+
934+
for _, notify := range []bool{false, true} {
935+
for _, sig := range sigs {
936+
t.Run(fmt.Sprintf("%s[notify=%t]", sig, notify), func(t *testing.T) {
937+
Ignore(sig)
938+
if notify {
939+
c := make(chan os.Signal, 1)
940+
Notify(c, sig)
941+
defer Stop(c)
942+
}
943+
Reset(sig)
944+
945+
if Ignored(sig) {
946+
t.Fatalf("expected %q to not be ignored", sig)
947+
}
948+
949+
// Child processes inherit the ignored status of signals, so verify that it
950+
// is indeed not ignored.
951+
testenv.MustHaveExec(t)
952+
953+
cmd := testenv.Command(t, os.Args[0], "-test.run=^TestResetIgnore$")
954+
cmd.Env = append(os.Environ(), "GO_TEST_RESET_IGNORE="+strconv.Itoa(int(sig)))
955+
err := cmd.Run()
956+
if _, ok := err.(*exec.ExitError); ok {
957+
t.Fatalf("expected %q to not be ignored in child process", sig)
958+
} else if err != nil {
959+
t.Fatalf("child process failed to launch: %v", err)
960+
}
961+
})
962+
}
963+
}
964+
}
965+
966+
func resetIgnoreTestProgram(t *testing.T, sig os.Signal) {
967+
if Ignored(sig) {
968+
os.Exit(1)
969+
}
970+
os.Exit(0)
971+
}

0 commit comments

Comments
 (0)