-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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/nsqlookupd: exit when tcp server accept error #1138
Closed
andyxning
wants to merge
1
commit into
nsqio:master
from
andyxning:exit_when_tcp_server_accept_error
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,6 +74,8 @@ type NSQD struct { | |
exitChan chan int | ||
waitGroup util.WaitGroupWrapper | ||
|
||
shuttingDown bool | ||
|
||
ci *clusterinfo.ClusterInfo | ||
} | ||
|
||
|
@@ -263,7 +265,9 @@ func (n *NSQD) Main() { | |
|
||
tcpServer := &tcpServer{ctx: ctx} | ||
n.waitGroup.Wrap(func() { | ||
protocol.TCPServer(n.tcpListener, tcpServer, n.logf) | ||
if err := protocol.TCPServer(n.tcpListener, tcpServer, n.logf); err != nil { | ||
go n.Exit() | ||
} | ||
}) | ||
httpServer := newHTTPServer(ctx, false, n.getOpts().TLSRequired == TLSRequired) | ||
n.waitGroup.Wrap(func() { | ||
|
@@ -436,6 +440,13 @@ func (n *NSQD) Exit() { | |
} | ||
|
||
n.Lock() | ||
|
||
if n.shuttingDown { | ||
n.Unlock() | ||
return | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should unlock if returning here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. |
||
} | ||
n.shuttingDown = true | ||
|
||
err := n.PersistMetadata() | ||
if err != nil { | ||
n.logf(LOG_ERROR, "failed to persist metadata - %s", err) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest checking the exiting flag here, and if it is not already set, then go Exit().
The flag variable could be an atomic int, to avoid the need to take a lock.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(this suggestion would require Exit() to set the flag as the first thing it does)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest we keep the check logic be within the
Exit
function. With this we should not take care about the internal exit status flag check beforeExit
is called in other places. And we should keepExit
function to be called only once. Without lock protection, even with atomic values, theset and check
logic exists race condition.With setting and checking separated with aotmic values, race condition may happens. For example, a normal exit and accept error exit happens simultaneously, before the normal exit call set the Exit flag, accept error exit checks it and then call Exit again. Although nsqd will exit correctly but Exit is called more than once.
With lock like this. Exit function logic except closing listeners will only be called once.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, lock consumption should be ok and will not invoke any new performance problems, because all this happens under exit phase.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason I suggested the exiting flag early on is because deciding to call Exit here could be as simple as:
(but there still is the issue of exiting with an error code)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed that the atomic would be slightly cleaner, but I prefer directing flow through the existing machinery that's setup to manage the lifecycle of the service to ensure that, whatever
svc.Run
is doing, it gets done (rather than introducing a new exceptional path).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah ... calling NSQD.Exit() cleans up but does not actually exit ... go-svc needs to be involved anyway
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one more cheesy idea to avoid needing to deal with go-svc:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Meh, let's just do it the "right" way.