New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: os/signal: Provide a new error type to communicate the signal triggered context cancellation #60756
Comments
I think this should be named Actually, err := cmd.Wait()
if errors.Is(err, os.SignalError{Signal: syscall.SIGABRT}) {
// err indicates termination due to signal SIGABRT — program probably failed an assertion
…
} |
@bcmills I agree that
But I think this can work if we use a generic error message, and change the |
In practice it always is:
So I'm not worried about somebody stuffing an incomparable |
^ wrong, see https://go.dev/play/p/qpyJXU05CFl |
|
hmm you are right. tested on playground: https://go.dev/play/p/qpyJXU05CFl but another issue with making the nonpointer type to implement var se *os.SignalError // they thought it would be the pointer type
if errors.As(err, &se) {
// they'd never get here
} the solution to that is to also implement |
To be clear, I think it would be ok for the type implementing ( |
But if we use pointer type, |
The error type can provide an explicit |
yes but then you need to deal with the comparable check of which kind of goes back to the same question as |
You don't need to check whether the |
That's not really in the "contract" of the API (https://pkg.go.dev/os#Signal definition doesn't guarantee comparability). You are essentially depending on implementation details to not panic. Although this is very very unlikely to happen in real life, hypothetically someone could define a type implementing |
@bcmills do you mind if I leave the Basically the revised proposal would be to add // TODO: what's a generic doc comment to describe it here that's appropriate?
type SignalError struct {
Signal Signal
}
func (se *SignalError) Error() string {
return "received signal " + se.Signal.String() // I think this error message is generic enough, in signal.NotifyContext I can just use wrap the error again with prefix of "context canceled by "
} |
@fishy, I started working on this in parallel too. When I saw you worked on it too, I rebased with your changes. Please take a look at my changes here: Regarding using a pointer for SignalError, I think we shouldn't. /cc @ianlancetaylor (on the original implementation of NotifyContext you shared some thoughts/ideas on how this type could be named; you might want to take a look). |
@henvic to reiterate #60756 (comment), the problem of allowing non-pointer type to implement |
I've created a new change request here https://go-review.googlesource.com/c/go/+/579375 |
Change https://go.dev/cl/579375 mentions this issue: |
Change https://go.dev/cl/580695 mentions this issue: |
This comes from the need to fix #60733.
In order to provide the information of which signal triggered context cancellation in
signal.NotifyContext
usingcontext.Cause
, we need a new error type (as that's the only type allowed by context cause) to communicate that info back to the user.The proposal is to define it as a simple struct that can only be used by pointer as an error:
So users can inspect the signal via code like:
The text was updated successfully, but these errors were encountered: