diff options
| author | Michael Pratt <mpratt@google.com> | 2026-01-08 18:36:45 -0500 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2026-02-24 10:27:05 -0800 |
| commit | c2fabf1a268f0480d78bede82fc6a9d3a63fcace (patch) | |
| tree | 148d9ac5f1d545e37d31e21cd6a121097fdccd09 /src/runtime | |
| parent | 01b795bc4c6ea54648c60ade6691ad306c0405d3 (diff) | |
| download | go-c2fabf1a268f0480d78bede82fc6a9d3a63fcace.tar.xz | |
os/signal: completely ignore bogus signals
signal.Notify should ignore bogus signals (syscall.Signals with invalid
signal numbers). Ignoring means Notify/Stop/Reset all work fine, but the
channel will never receive any events.
Today, Stop hangs if Notify has only ever been called with bogus signals
because Stop assumes that runtime.signal_recv is running if a handler is
present. Notify unconditionally registers the handler, but only starts
signal_recv if the signal was valid.
Address this by avoiding registering the handler at all if the signal is
bogus.
We additionally add a bogus signal check to cancel (used by
Ignore/Reset). Currently those are calling into the runtime
signal_ignore and signal_disable, which do ignore bogus signals, but is
now inconsistent with the rest of the package.
For #77076.
Change-Id: I6a6a636c27c41a158e203bbf470be5f1f3f631bd
Reviewed-on: https://go-review.googlesource.com/c/go/+/735040
Reviewed-by: Cherry Mui <cherryyz@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Auto-Submit: Michael Pratt <mpratt@google.com>
Diffstat (limited to 'src/runtime')
| -rw-r--r-- | src/runtime/signal_test.go | 26 | ||||
| -rw-r--r-- | src/runtime/testdata/testprog/signal_bogus.go | 29 |
2 files changed, 55 insertions, 0 deletions
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") +} |
