Go

Go

Made by DeepSource

Found trapping a signal that cannot be trapped SCC-SA1016

Anti-pattern
Major
Autofix

A process cannot intercept all signals. Specifically, on UNIX-like systems, the syscall.SIGKILL and syscall.SIGSTOP signals cannot be captured by the process but handled directly by the kernel. It is, therefore, pointless to try and handle these signals.

Bad practice

func main() {
    // Set up channel on which to send signal notifications.
    // We must use a buffered channel or risk missing the signal
    // if we're not ready to receive when the signal is sent.
    c := make(chan os.Signal, 1)
    signal.Notify(c,
        os.Interrupt,
        syscall.SIGKILL, // cannot be captured
        syscall.SIGSTOP, // cannot be captured
    )

    // Block until a signal is received.
    s := <-c
    fmt.Println("Got signal:", s)
}

Recommended

func main() {
    // Set up channel on which to send signal notifications.
    // We must use a buffered channel or risk missing the signal
    // if we're not ready to receive when the signal is sent.
    c := make(chan os.Signal, 1)
    signal.Notify(c, os.Interrupt)

    // Block until a signal is received.
    s := <-c
    fmt.Println("Got signal:", s)
}