errors.As calls SignalError.As when it cannot match the concrete type directly, e.g. for `var target SignalError; errors.As(err, &target)`. The previous implementation matched target's type and returned true but never wrote to it, so callers got a successful errors.As result with a zero-valued SignalError and no indication the signal was lost. Add a regression test asserting the target is actually populated.
101 lines
3.0 KiB
Go
101 lines
3.0 KiB
Go
package run
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
)
|
|
|
|
// ContextHandler returns an actor, i.e. an execute and interrupt func, that
|
|
// terminates when the provided context is canceled.
|
|
func ContextHandler(ctx context.Context) (execute func() error, interrupt func(error)) {
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
return func() error {
|
|
<-ctx.Done()
|
|
return ctx.Err()
|
|
}, func(error) {
|
|
cancel()
|
|
}
|
|
}
|
|
|
|
// SignalHandler returns an actor, i.e. an execute and interrupt func, that
|
|
// terminates with ErrSignal when the process receives one of the provided
|
|
// signals, or with ctx.Error() when the parent context is canceled. If no
|
|
// signals are provided, the actor will terminate on any signal, per
|
|
// [signal.Notify].
|
|
func SignalHandler(ctx context.Context, signals ...os.Signal) (execute func() error, interrupt func(error)) {
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
return func() error {
|
|
testc := getTestSigChan(ctx)
|
|
sigc := make(chan os.Signal, 1)
|
|
signal.Notify(sigc, signals...)
|
|
defer signal.Stop(sigc)
|
|
select {
|
|
case sig := <-testc:
|
|
return &SignalError{Signal: sig}
|
|
case sig := <-sigc:
|
|
return &SignalError{Signal: sig}
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
}
|
|
}, func(error) {
|
|
cancel()
|
|
}
|
|
}
|
|
|
|
type testSigChanKey struct{}
|
|
|
|
func getTestSigChan(ctx context.Context) <-chan os.Signal {
|
|
c, _ := ctx.Value(testSigChanKey{}).(<-chan os.Signal) // can be nil
|
|
return c
|
|
}
|
|
|
|
func putTestSigChan(ctx context.Context, c <-chan os.Signal) context.Context {
|
|
return context.WithValue(ctx, testSigChanKey{}, c)
|
|
}
|
|
|
|
// SignalError is returned by the signal handler's execute function when it
|
|
// terminates due to a received signal.
|
|
//
|
|
// SignalError has a design error that impacts comparison with errors.As.
|
|
// Callers should prefer using errors.Is(err, ErrSignal) to check for signal
|
|
// errors, and should only use errors.As in the rare case that they need to
|
|
// program against the specific os.Signal value.
|
|
type SignalError struct {
|
|
Signal os.Signal
|
|
}
|
|
|
|
// Error implements the error interface.
|
|
//
|
|
// It was a design error to define this method on a value receiver rather than a
|
|
// pointer receiver. For compatibility reasons it won't be changed.
|
|
func (e SignalError) Error() string {
|
|
return fmt.Sprintf("received signal %s", e.Signal)
|
|
}
|
|
|
|
// Is addresses a design error in the SignalError type, so that errors.Is with
|
|
// ErrSignal will return true.
|
|
func (e SignalError) Is(err error) bool {
|
|
return errors.Is(err, ErrSignal)
|
|
}
|
|
|
|
// As fixes a design error in the SignalError type, so that errors.As with the
|
|
// literal `&SignalError{}` will return true and, per the errors.As contract,
|
|
// populate target with the underlying signal.
|
|
//
|
|
// errors.As requires target to be a non-nil pointer, so the only target type
|
|
// that can actually be assigned here is *SignalError.
|
|
func (e SignalError) As(target interface{}) bool {
|
|
t, ok := target.(*SignalError)
|
|
if !ok {
|
|
return false
|
|
}
|
|
*t = e
|
|
return true
|
|
}
|
|
|
|
// ErrSignal is returned by SignalHandler when a signal triggers termination.
|
|
var ErrSignal = errors.New("signal error")
|