signal.Notify is given a channel without a buffer, so signals that arrive while nobody receives are lost

tx0/go:unbuffered-signal-channeldefaultwarningbug, concurrency

What it finds

c := make(chan os.Signal); signal.Notify(c, os.Interrupt): the signal package does not block when it delivers a signal, so with no buffer a signal that arrives while the program is not waiting in a receive is dropped. A buffer of one is enough for a program that waits for one signal.

The channel must be a local variable that the same function declares with make of a channel type and no size, or a size of the literal 0, and passes to Notify of os/signal, resolved through the imports of the file.

From the rule's source, unbuffered_signal_channel.rs.

Example from the rule's tests
package p import (	"os"	"os/signal") func f() {	stop := make(chan os.Signal)	signal.Notify(stop, os.Interrupt)tx0 reports: stop has no buffer, so a signal that arrives while nobody receives is lost	<-stop	ok := make(chan os.Signal, 1)	signal.Notify(ok, os.Interrupt)	<-ok}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/go:unbuffered-signal-channel" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/go:unbuffered-signal-channel] reason="..."

tx0 explain tx0/go:unbuffered-signal-channel shows the same in the terminal. Found a false positive? Report it.