Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nsqd: handle repeated calls to Exit() #1331

Merged
merged 1 commit into from
Apr 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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