Skip to content

Commit 9f16b60

Browse files
committed
nsqd: handle repeated calls to Exit()
1 parent 74f0dca commit 9f16b60

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

apps/nsqd/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ func (p *program) Start() error {
8787
// we don't want to un-register our sigterm handler which would
8888
// cause default go behavior to apply
8989
for range signalChan {
90-
p.nsqd.TermSignal()
90+
p.once.Do(func() {
91+
p.nsqd.Exit()
92+
})
9193
}
9294
}()
9395
signal.Notify(signalChan, syscall.SIGTERM)

nsqd/nsqd.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type NSQD struct {
5050

5151
dl *dirlock.DirLock
5252
isLoading int32
53+
isExiting int32
5354
errValue atomic.Value
5455
startTime time.Time
5556

@@ -402,13 +403,11 @@ func (n *NSQD) PersistMetadata() error {
402403
return nil
403404
}
404405

405-
// TermSignal handles a SIGTERM calling Exit
406-
// This is a noop after first call
407-
func (n *NSQD) TermSignal() {
408-
n.Exit()
409-
}
410-
411406
func (n *NSQD) Exit() {
407+
if !atomic.CompareAndSwapInt32(&n.isExiting, 0, 1) {
408+
// avoid double call
409+
return
410+
}
412411
if n.tcpListener != nil {
413412
n.tcpListener.Close()
414413
}

0 commit comments

Comments
 (0)