Skip to content

Commit cb9cccf

Browse files
MarcoPoloJacobOaks
andauthored
Call signal.Stop when signalReceivers is stopped (#1198)
fixes #1197 --------- Co-authored-by: Jacob Oaks <joaks@uber.com>
1 parent 696ed9a commit cb9cccf

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

signal.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ func (sig ShutdownSignal) String() string {
4646

4747
func newSignalReceivers() signalReceivers {
4848
return signalReceivers{
49-
notify: signal.Notify,
50-
signals: make(chan os.Signal, 1),
49+
notify: signal.Notify,
50+
stopNotify: signal.Stop,
51+
signals: make(chan os.Signal, 1),
5152
}
5253
}
5354

@@ -64,7 +65,8 @@ type signalReceivers struct {
6465
finished chan struct{}
6566

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

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

122125
// if the relayer is not running; return nil error
123126
if !recv.running() {

signal_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ func TestSignal(t *testing.T) {
100100
}
101101
}()
102102
}
103+
var stopCalledTimes int
104+
recv.stopNotify = func(ch chan<- os.Signal) {
105+
stopCalledTimes++
106+
}
103107
ctx, cancel := context.WithCancel(context.Background())
104108
defer cancel()
105109
recv.Start(ctx)
@@ -110,6 +114,7 @@ func TestSignal(t *testing.T) {
110114
sig := <-recv.Wait()
111115
require.Equal(t, syscall.SIGTERM, sig.Signal)
112116
require.NoError(t, recv.Stop(ctx))
117+
require.Equal(t, 1, stopCalledTimes)
113118
close(stub)
114119
})
115120
})

0 commit comments

Comments
 (0)