Skip to content

os: Process.Signal on the current PID should signal the current thread  #19326

@bcmills

Description

@bcmills

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions