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:
Dave Collins 2014-07-22 08:21:58 -05:00
parent 293d71e9c3
commit 1184a4d8d1

View file

@ -24,11 +24,26 @@ func mainInterruptHandler() {
// SIGINT (Ctrl+C) is received. // SIGINT (Ctrl+C) is received.
var interruptCallbacks []func() 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 { for {
select { select {
case <-interruptChannel: 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...") btcdLog.Infof("Received SIGINT (Ctrl+C). Shutting down...")
// run handlers in LIFO order.
// Run handlers in LIFO order.
for i := range interruptCallbacks { for i := range interruptCallbacks {
idx := len(interruptCallbacks) - 1 - i idx := len(interruptCallbacks) - 1 - i
callback := interruptCallbacks[idx] callback := interruptCallbacks[idx]
@ -36,9 +51,17 @@ func mainInterruptHandler() {
} }
// Signal the main goroutine to shutdown. // Signal the main goroutine to shutdown.
shutdownChannel <- struct{}{} go func() {
shutdownChannel <- struct{}{}
}()
case handler := <-addHandlerChannel: case handler := <-addHandlerChannel:
// The shutdown signal has already been received, so
// just invoke and new handlers immediately.
if isShutdown {
handler()
}
interruptCallbacks = append(interruptCallbacks, handler) interruptCallbacks = append(interruptCallbacks, handler)
} }
} }