- // The stop function unregisters the signal behavior, which, like signal.Reset,
- // may restore the default behavior for a given signal. For example, the default
- // behavior of a Go program receiving os.Interrupt is to exit. Calling
- // NotifyContext(parent, os.Interrupt) will change the behavior to cancel
- // the returned context. Future interrupts received will not trigger the default
- // (exit) behavior until the returned stop function is called.
- //
- // The stop function releases resources associated with it, so code should
- // call stop as soon as the operations running in this Context complete and
- // signals no longer need to be diverted to the context.
- func NotifyContext(parent context.Context, signals ...os.Signal) (ctx context.Context, stop context.CancelFunc) {
- ctx, cancel := context.WithCancel(parent)
- c := &signalCtx{
- Context: ctx,
- cancel: cancel,
- signals: signals,
- }
- c.ch = make(chan os.Signal, 1)
- Notify(c.ch, c.signals...)
- if ctx.Err() == nil {
- go func() {