Skip to content

Commit

Permalink
dcrd: handle signal SIGTERM (#688) (#156)
Browse files Browse the repository at this point in the history
When an OS reboots or shuts down, it sends all processes SIGTERM before
sending SIGKILL.  This allows dcrd to do a proper shutdown which most
importantly closes the databases.
  • Loading branch information
dajohi authored and alexlyp committed May 6, 2016
1 parent 0b6f881 commit 7701a7a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
15 changes: 10 additions & 5 deletions signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ var interruptChannel chan os.Signal
// to be invoked on SIGINT (Ctrl+C) signals.
var addHandlerChannel = make(chan func())

// signals defines the default signals to catch in order to do a proper
// shutdown.
var signals = []os.Signal{os.Interrupt}

// mainInterruptHandler listens for SIGINT (Ctrl+C) signals on the
// interruptChannel and invokes the registered interruptCallbacks accordingly.
// It also listens for callback registration. It must be run as a goroutine.
Expand All @@ -33,16 +37,17 @@ func mainInterruptHandler() {

for {
select {
case <-interruptChannel:
case sig := <-interruptChannel:
// Ignore more than one shutdown signal.
if isShutdown {
dcrdLog.Infof("Received SIGINT (Ctrl+C). " +
"Already shutting down...")
dcrdLog.Infof("Received signal (%s). "+
"Already shutting down...", sig)
continue
}

isShutdown = true
dcrdLog.Infof("Received SIGINT (Ctrl+C). Shutting down...")
dcrdLog.Infof("Received signal (%s). Shutting down...",
sig)

// Run handlers in LIFO order.
for i := range interruptCallbacks {
Expand Down Expand Up @@ -75,7 +80,7 @@ func addInterruptHandler(handler func()) {
// all other callbacks and exits if not already done.
if interruptChannel == nil {
interruptChannel = make(chan os.Signal, 1)
signal.Notify(interruptChannel, os.Interrupt)
signal.Notify(interruptChannel, signals...)
go mainInterruptHandler()
}

Expand Down
16 changes: 16 additions & 0 deletions signalsigterm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) 2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

// +build darwin dragonfly freebsd linux netbsd openbsd solaris

package main

import (
"os"
"syscall"
)

func init() {
signals = []os.Signal{os.Interrupt, syscall.SIGTERM}
}

0 comments on commit 7701a7a

Please sign in to comment.