Prevent hang on shutdown race.
This commit prevents a race that could happen on shutdown if a sigint was received between adding the first the interrupt handler and and future handlers. This code is loosely based on initial pull request #154 which was not accepted due to introducing a race of its own. Thanks to @tuxcanfly for the intial pull request and finding the issue.
This commit is contained in:
parent
293d71e9c3
commit
1184a4d8d1
1 changed files with 25 additions and 2 deletions
25
signal.go
25
signal.go
|
@ -24,11 +24,26 @@ func mainInterruptHandler() {
|
|||
// SIGINT (Ctrl+C) is received.
|
||||
var interruptCallbacks []func()
|
||||
|
||||
// isShutdown is a flag which is used to indicate whether or not
|
||||
// the shutdown signal has already been received and hence any future
|
||||
// attempts to add a new interrupt handler should invoke them
|
||||
// immediately.
|
||||
var isShutdown bool
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-interruptChannel:
|
||||
// Ignore more than one shutdown signal.
|
||||
if isShutdown {
|
||||
btcdLog.Infof("Received SIGINT (Ctrl+C). " +
|
||||
"Already shutting down...")
|
||||
continue
|
||||
}
|
||||
|
||||
isShutdown = true
|
||||
btcdLog.Infof("Received SIGINT (Ctrl+C). Shutting down...")
|
||||
// run handlers in LIFO order.
|
||||
|
||||
// Run handlers in LIFO order.
|
||||
for i := range interruptCallbacks {
|
||||
idx := len(interruptCallbacks) - 1 - i
|
||||
callback := interruptCallbacks[idx]
|
||||
|
@ -36,9 +51,17 @@ func mainInterruptHandler() {
|
|||
}
|
||||
|
||||
// Signal the main goroutine to shutdown.
|
||||
go func() {
|
||||
shutdownChannel <- struct{}{}
|
||||
}()
|
||||
|
||||
case handler := <-addHandlerChannel:
|
||||
// The shutdown signal has already been received, so
|
||||
// just invoke and new handlers immediately.
|
||||
if isShutdown {
|
||||
handler()
|
||||
}
|
||||
|
||||
interruptCallbacks = append(interruptCallbacks, handler)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue