diff options
author | Felix Lange <fjl@twurst.com> | 2015-07-06 21:01:13 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2015-07-06 21:06:11 +0800 |
commit | 5615fc47149ea5db6ad6f5b1b716e5af9900f848 (patch) | |
tree | 461b31ea7ea4dafb0b2658dd11a0dbcc3f04d88f /cmd/utils/cmd.go | |
parent | aa4502060b50733a3a82a0ab575eac0731cdb7ca (diff) | |
download | go-tangerine-5615fc47149ea5db6ad6f5b1b716e5af9900f848.tar.gz go-tangerine-5615fc47149ea5db6ad6f5b1b716e5af9900f848.tar.zst go-tangerine-5615fc47149ea5db6ad6f5b1b716e5af9900f848.zip |
cmd/geth, cmd/utils: improve interrupt handling
The new strategy for interrupts is to handle them explicitly.
Ethereum.Stop is now only called once, even if multiple interrupts
are sent. Interrupting ten times in a row forces a panic.
Fixes #869
Fixes #1359
Diffstat (limited to 'cmd/utils/cmd.go')
-rw-r--r-- | cmd/utils/cmd.go | 50 |
1 files changed, 16 insertions, 34 deletions
diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go index f7520a8e4..33a6c1cb2 100644 --- a/cmd/utils/cmd.go +++ b/cmd/utils/cmd.go @@ -46,29 +46,6 @@ const ( var interruptCallbacks = []func(os.Signal){} -// Register interrupt handlers callbacks -func RegisterInterrupt(cb func(os.Signal)) { - interruptCallbacks = append(interruptCallbacks, cb) -} - -// go routine that call interrupt handlers in order of registering -func HandleInterrupt() { - c := make(chan os.Signal, 1) - go func() { - signal.Notify(c, os.Interrupt) - for sig := range c { - glog.V(logger.Error).Infof("Shutting down (%v) ... \n", sig) - RunInterruptCallbacks(sig) - } - }() -} - -func RunInterruptCallbacks(sig os.Signal) { - for _, cb := range interruptCallbacks { - cb(sig) - } -} - func openLogFile(Datadir string, filename string) *os.File { path := common.AbsolutePath(Datadir, filename) file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) @@ -149,19 +126,24 @@ func StartEthereum(ethereum *eth.Ethereum) { if err := ethereum.Start(); err != nil { Fatalf("Error starting Ethereum: %v", err) } - RegisterInterrupt(func(sig os.Signal) { - ethereum.Stop() - logger.Flush() - }) -} - -func StartEthereumForTest(ethereum *eth.Ethereum) { - glog.V(logger.Info).Infoln("Starting ", ethereum.Name()) - ethereum.StartForTest() - RegisterInterrupt(func(sig os.Signal) { + go func() { + sigc := make(chan os.Signal, 1) + signal.Notify(sigc, os.Interrupt) + defer signal.Stop(sigc) + <-sigc + glog.V(logger.Info).Infoln("Got interrupt, shutting down...") ethereum.Stop() logger.Flush() - }) + for i := 10; i > 0; i-- { + <-sigc + if i > 1 { + glog.V(logger.Info).Infoln("Already shutting down, please be patient.") + glog.V(logger.Info).Infoln("Interrupt", i-1, "more times to induce panic.") + } + } + glog.V(logger.Error).Infof("Force quitting: this might not end so well.") + panic("boom") + }() } func FormatTransactionData(data string) []byte { |