os/signal: make NotifyContext Cause match context.Canceled

Fixes #77639

Change-Id: I90e5d6f68c02749ac6e407f1c3565312efafb033
Reviewed-on: https://go-review.googlesource.com/c/go/+/779860
Reviewed-by: David Chase <drchase@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Florian Lehner <lehner.florian86@gmail.com>
Auto-Submit: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Ian Lance Taylor 2026-05-19 09:28:23 -07:00 committed by Gopher Robot
parent 37bce6617f
commit 8494d25c4c
2 changed files with 29 additions and 0 deletions

View file

@ -354,3 +354,10 @@ type signalError string
func (s signalError) Error() string {
return string(s)
}
func (s signalError) Is(target error) bool {
if target == context.Canceled {
return true
}
return false
}

View file

@ -924,3 +924,25 @@ func TestSignalTrace(t *testing.T) {
close(quit)
<-done
}
// #77639.
func TestNotifyContextCause(t *testing.T) {
ctx, stop := NotifyContext(t.Context(), syscall.SIGINT)
defer stop()
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
<-ctx.Done()
err := ctx.Err()
if err == nil {
t.Error("ctx.Err returned nil")
} else if !errors.Is(err, context.Canceled) {
t.Errorf("error %v is not context.Canceled", err)
}
err = context.Cause(ctx)
if err == nil {
t.Error("context.Cause returned nil")
} else if !errors.Is(err, context.Canceled) {
t.Errorf("cause %v is not context.Canceled", err)
}
}