]> Cypherpunks repositories - gostls13.git/commit
os/signal: make NotifyContext cancel the context with a cause
authorFilippo Valsorda <filippo@golang.org>
Tue, 18 Nov 2025 16:19:04 +0000 (17:19 +0100)
committerGopher Robot <gobot@golang.org>
Thu, 20 Nov 2025 21:01:32 +0000 (13:01 -0800)
commitc1b7112af8331d37e33be521d2d8baa552945971
treeb047983fbb754b936b728911739bed39efde264b
parentca37d24e0b9369b8086959df5bc230b38bf98636
os/signal: make NotifyContext cancel the context with a cause

This is especially useful when combined with the nesting semantics of
context.Cause, and with errgroup's use of CancelCauseFunc.

For example, with the following code

ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
serveGroup, ctx := errgroup.WithContext(ctx)

calling context.Cause(ctx) after serveGroup.Wait() will return either
"interrupt signal received" (if that happens first) or the error from
serveGroup.

Change-Id: Ie181f5f84269f6e39defdad2d5fd8ead6a6a6964
Reviewed-on: https://go-review.googlesource.com/c/go/+/721700
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Mark Freeman <markfreeman@google.com>
Reviewed-by: Sean Liao <sean@liao.dev>
Auto-Submit: Filippo Valsorda <filippo@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Commit-Queue: Junyang Shao <shaojunyang@google.com>
Reviewed-by: Junyang Shao <shaojunyang@google.com>
doc/next/6-stdlib/99-minor/os/signal/notifycontext.md [new file with mode: 0644]
src/os/signal/signal.go
src/os/signal/signal_test.go