From 9f16b60a4f04d6205ecb159bcef135fc8d7712d2 Mon Sep 17 00:00:00 2001 From: Jehiah Czebotar Date: Tue, 23 Mar 2021 09:52:34 -0400 Subject: [PATCH] nsqd: handle repeated calls to Exit() --- apps/nsqd/main.go | 4 +++- nsqd/nsqd.go | 11 +++++------ 2 files changed, 8 insertions(+), 7 deletions(-) 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 550647d6e..f7b8691f4 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 @@ -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() }