Skip to content

Commit

Permalink
nsqd: handle repeated calls to Exit()
Browse files Browse the repository at this point in the history
  • Loading branch information
jehiah committed Apr 4, 2021
1 parent 74f0dca commit 9f16b60
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
4 changes: 3 additions & 1 deletion apps/nsqd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ func (p *program) Start() error {
// we don't want to un-register our sigterm handler which would
// cause default go behavior to apply
for range signalChan {
p.nsqd.TermSignal()
p.once.Do(func() {
p.nsqd.Exit()
})
}
}()
signal.Notify(signalChan, syscall.SIGTERM)
Expand Down
11 changes: 5 additions & 6 deletions nsqd/nsqd.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type NSQD struct {

dl *dirlock.DirLock
isLoading int32
isExiting int32
errValue atomic.Value
startTime time.Time

Expand Down Expand Up @@ -402,13 +403,11 @@ func (n *NSQD) PersistMetadata() error {
return nil
}

// TermSignal handles a SIGTERM calling Exit
// This is a noop after first call
func (n *NSQD) TermSignal() {
n.Exit()
}

func (n *NSQD) Exit() {
if !atomic.CompareAndSwapInt32(&n.isExiting, 0, 1) {
// avoid double call
return
}
if n.tcpListener != nil {
n.tcpListener.Close()
}
Expand Down

0 comments on commit 9f16b60

Please sign in to comment.