aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/os/signal/signal.go24
-rw-r--r--src/runtime/signal_test.go26
-rw-r--r--src/runtime/testdata/testprog/signal_bogus.go29
3 files changed, 73 insertions, 6 deletions
diff --git a/src/os/signal/signal.go b/src/os/signal/signal.go
index 70a91055e2..e5e4816ffe 100644
--- a/src/os/signal/signal.go
+++ b/src/os/signal/signal.go
@@ -54,6 +54,10 @@ func cancel(sigs []os.Signal, action func(int)) {
defer handlers.Unlock()
remove := func(n int) {
+ if n < 0 {
+ return
+ }
+
var zerohandler handler
for c, h := range handlers.m {
@@ -127,19 +131,27 @@ func Notify(c chan<- os.Signal, sig ...os.Signal) {
handlers.Lock()
defer handlers.Unlock()
- h := handlers.m[c]
- if h == nil {
- if handlers.m == nil {
- handlers.m = make(map[chan<- os.Signal]*handler)
+ // Lazily create the handler. If all of the signals are bogus there is
+ // no need to install a handler at all.
+ getHandler := func() *handler {
+ h := handlers.m[c]
+ if h == nil {
+ if handlers.m == nil {
+ handlers.m = make(map[chan<- os.Signal]*handler)
+ }
+ h = new(handler)
+ handlers.m[c] = h
}
- h = new(handler)
- handlers.m[c] = h
+
+ return h
}
add := func(n int) {
if n < 0 {
return
}
+
+ h := getHandler()
if !h.want(n) {
h.set(n)
if handlers.ref[n] == 0 {
diff --git a/src/runtime/signal_test.go b/src/runtime/signal_test.go
new file mode 100644
index 0000000000..af37eb68aa
--- /dev/null
+++ b/src/runtime/signal_test.go
@@ -0,0 +1,26 @@
+// Copyright 2026 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package runtime_test
+
+import (
+ "runtime"
+ "testing"
+)
+
+func TestSignalBogus(t *testing.T) {
+ if runtime.GOOS == "plan9" {
+ t.Skip("No syscall.Signal on plan9")
+ }
+
+ // This test more properly belongs in os/signal, but it depends on
+ // containing the only call to signal.Notify in the process, so it must
+ // run as an isolated subprocess, which is simplest with testprog.
+ t.Parallel()
+ output := runTestProg(t, "testprog", "SignalBogus")
+ want := "OK\n"
+ if output != want {
+ t.Fatalf("output is not %q\n%s", want, output)
+ }
+}
diff --git a/src/runtime/testdata/testprog/signal_bogus.go b/src/runtime/testdata/testprog/signal_bogus.go
new file mode 100644
index 0000000000..a372277d41
--- /dev/null
+++ b/src/runtime/testdata/testprog/signal_bogus.go
@@ -0,0 +1,29 @@
+// Copyright 2026 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build !plan9
+
+package main
+
+import (
+ "os"
+ "os/signal"
+ "syscall"
+)
+
+func init() {
+ register("SignalBogus", SignalBogus)
+}
+
+// signal.Notify should effectively ignore bogus signal numbers. Never writing
+// to the channel, but otherwise allowing Notify/Stop as normal.
+//
+// This is a regression test for https://go.dev/issue/77076, where bogus
+// signals used to make Stop hang if there were no real signals installed.
+func SignalBogus() {
+ ch := make(chan os.Signal, 1)
+ signal.Notify(ch, syscall.Signal(0xdead))
+ signal.Stop(ch)
+ println("OK")
+}