-
Notifications
You must be signed in to change notification settings - Fork 18.3k
Description
The Go standard library currently lacks an equivalent to the standard C raise
function.
The closest portable analogue is something like:
func raise(sig os.Signal) error {
p, err := os.FindProcess(os.Getpid())
if err != nil {
return err
}
return p.Signal(sig)
}
Unfortunately, this pattern is somewhat error-prone: on many platforms (e.g. Linux), it can deliver the signal to any thread in the process group. If the purpose of raising the signal is to produce a stack trace or trigger the invocation of a C fatal-signal handler (e.g. by raising SIGQUIT
or SIGABRT
), that can result in the handler executing on a thread unrelated to the failure.
It's tempting to request a new function call, os.Raise
, for this purpose, but it seems like we could address this use-case automatically without an API change. We could have (*os.Process).Signal
check whether the Process
in question is the Go program itself and raise the signal synchronously to the current thread (e.g. by using the tgkill
syscall on Linux, or calling pthread_kill
or raise
in cgo-enabled builds).