Skip to content

Commit 5615fc4

Browse files
committed
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
1 parent aa45020 commit 5615fc4

File tree

2 files changed

+17
-37
lines changed

2 files changed

+17
-37
lines changed

cmd/geth/main.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,6 @@ func main() {
347347
}
348348

349349
func run(ctx *cli.Context) {
350-
utils.HandleInterrupt()
351350
cfg := utils.MakeEthConfig(ClientIdentifier, nodeNameVersion, ctx)
352351
ethereum, err := eth.New(cfg)
353352
if err != nil {
@@ -527,10 +526,9 @@ func blockRecovery(ctx *cli.Context) {
527526

528527
func startEth(ctx *cli.Context, eth *eth.Ethereum) {
529528
// Start Ethereum itself
530-
531529
utils.StartEthereum(eth)
532-
am := eth.AccountManager()
533530

531+
am := eth.AccountManager()
534532
account := ctx.GlobalString(utils.UnlockedAccountFlag.Name)
535533
accounts := strings.Split(account, " ")
536534
for i, account := range accounts {

cmd/utils/cmd.go

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,6 @@ const (
4646

4747
var interruptCallbacks = []func(os.Signal){}
4848

49-
// Register interrupt handlers callbacks
50-
func RegisterInterrupt(cb func(os.Signal)) {
51-
interruptCallbacks = append(interruptCallbacks, cb)
52-
}
53-
54-
// go routine that call interrupt handlers in order of registering
55-
func HandleInterrupt() {
56-
c := make(chan os.Signal, 1)
57-
go func() {
58-
signal.Notify(c, os.Interrupt)
59-
for sig := range c {
60-
glog.V(logger.Error).Infof("Shutting down (%v) ... \n", sig)
61-
RunInterruptCallbacks(sig)
62-
}
63-
}()
64-
}
65-
66-
func RunInterruptCallbacks(sig os.Signal) {
67-
for _, cb := range interruptCallbacks {
68-
cb(sig)
69-
}
70-
}
71-
7249
func openLogFile(Datadir string, filename string) *os.File {
7350
path := common.AbsolutePath(Datadir, filename)
7451
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
@@ -149,19 +126,24 @@ func StartEthereum(ethereum *eth.Ethereum) {
149126
if err := ethereum.Start(); err != nil {
150127
Fatalf("Error starting Ethereum: %v", err)
151128
}
152-
RegisterInterrupt(func(sig os.Signal) {
153-
ethereum.Stop()
154-
logger.Flush()
155-
})
156-
}
157-
158-
func StartEthereumForTest(ethereum *eth.Ethereum) {
159-
glog.V(logger.Info).Infoln("Starting ", ethereum.Name())
160-
ethereum.StartForTest()
161-
RegisterInterrupt(func(sig os.Signal) {
129+
go func() {
130+
sigc := make(chan os.Signal, 1)
131+
signal.Notify(sigc, os.Interrupt)
132+
defer signal.Stop(sigc)
133+
<-sigc
134+
glog.V(logger.Info).Infoln("Got interrupt, shutting down...")
162135
ethereum.Stop()
163136
logger.Flush()
164-
})
137+
for i := 10; i > 0; i-- {
138+
<-sigc
139+
if i > 1 {
140+
glog.V(logger.Info).Infoln("Already shutting down, please be patient.")
141+
glog.V(logger.Info).Infoln("Interrupt", i-1, "more times to induce panic.")
142+
}
143+
}
144+
glog.V(logger.Error).Infof("Force quitting: this might not end so well.")
145+
panic("boom")
146+
}()
165147
}
166148

167149
func FormatTransactionData(data string) []byte {

0 commit comments

Comments
 (0)