Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call signal.Stop when signalReceivers is stopped #1198

Merged
merged 3 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ func (sig ShutdownSignal) String() string {

func newSignalReceivers() signalReceivers {
return signalReceivers{
notify: signal.Notify,
signals: make(chan os.Signal, 1),
notify: signal.Notify,
stopNotify: signal.Stop,
signals: make(chan os.Signal, 1),
}
}

Expand All @@ -64,7 +65,8 @@ type signalReceivers struct {
finished chan struct{}

// this stub allows us to unit test signal relay functionality
notify func(c chan<- os.Signal, sig ...os.Signal)
notify func(c chan<- os.Signal, sig ...os.Signal)
stopNotify func(c chan<- os.Signal)

// last will contain a pointer to the last ShutdownSignal received, or
// nil if none, if a new channel is created by Wait or Done, this last
Expand Down Expand Up @@ -118,6 +120,7 @@ func (recv *signalReceivers) Start(ctx context.Context) {
func (recv *signalReceivers) Stop(ctx context.Context) error {
recv.m.Lock()
defer recv.m.Unlock()
recv.stopNotify(recv.signals)

// if the relayer is not running; return nil error
if !recv.running() {
Expand Down
6 changes: 6 additions & 0 deletions signal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package fx
import (
"context"
"os"
"sync/atomic"
"syscall"
"testing"

Expand Down Expand Up @@ -100,6 +101,10 @@ func TestSignal(t *testing.T) {
}
}()
}
var stopCalledTimes atomic.Uint32
MarcoPolo marked this conversation as resolved.
Show resolved Hide resolved
recv.stopNotify = func(ch chan<- os.Signal) {
stopCalledTimes.Add(1)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
recv.Start(ctx)
Expand All @@ -110,6 +115,7 @@ func TestSignal(t *testing.T) {
sig := <-recv.Wait()
require.Equal(t, syscall.SIGTERM, sig.Signal)
require.NoError(t, recv.Stop(ctx))
require.Equal(t, uint32(1), stopCalledTimes.Load())
close(stub)
})
})
Expand Down