diff --git a/apps/nsqd/main.go b/apps/nsqd/main.go index cd2121376..3e22d74a2 100644 --- a/apps/nsqd/main.go +++ b/apps/nsqd/main.go @@ -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) diff --git a/nsqd/nsqd.go b/nsqd/nsqd.go index 10fc4ad4c..d918b7856 100644 --- a/nsqd/nsqd.go +++ b/nsqd/nsqd.go @@ -50,6 +50,7 @@ type NSQD struct { dl *dirlock.DirLock isLoading int32 + isExiting int32 errValue atomic.Value startTime time.Time @@ -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() }