Skip to content

Commit

Permalink
Merge pull request #1331 from jehiah/exit_closed_channel_1331
Browse files Browse the repository at this point in the history
nsqd: handle repeated  calls to Exit()
  • Loading branch information
mreiferson authored Apr 4, 2021
2 parents 619ac10 + 9f16b60 commit bc798c6
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 @@ -401,13 +402,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 bc798c6

Please sign in to comment.