From eee6e044b77c448f9a4bdf27476e8bc54437be86 Mon Sep 17 00:00:00 2001 From: Peter Bourgon Date: Sat, 27 Jan 2024 21:06:40 +0100 Subject: [PATCH] Fix bug in SignalHandler (#27) --- actors.go | 3 ++- actors_test.go | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/actors.go b/actors.go index 10d50e2..ad6aed8 100644 --- a/actors.go +++ b/actors.go @@ -48,7 +48,8 @@ func SignalHandler(ctx context.Context, signals ...os.Signal) (execute func() er type testSigChanKey struct{} func getTestSigChan(ctx context.Context) <-chan os.Signal { - return ctx.Value(testSigChanKey{}).(<-chan os.Signal) // can be nil + c, _ := ctx.Value(testSigChanKey{}).(<-chan os.Signal) // can be nil + return c } func putTestSigChan(ctx context.Context, c <-chan os.Signal) context.Context { diff --git a/actors_test.go b/actors_test.go index bbb5efa..d8d7ea1 100644 --- a/actors_test.go +++ b/actors_test.go @@ -57,3 +57,11 @@ func TestSignalError(t *testing.T) { t.Errorf("errors.Is(err, ErrSignal): want %v, have %v", want, have) } } + +func TestSignalHandlerNil(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + var rg Group + rg.Add(SignalHandler(ctx, os.Interrupt)) + cancel() + t.Logf("%v", rg.Run()) +}