Skip to content

Commit

Permalink
cmd/geth, cmd/utils: improve interrupt handling
Browse files Browse the repository at this point in the history
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 ethereum#869
Fixes ethereum#1359
  • Loading branch information
fjl committed Jul 6, 2015
1 parent aa45020 commit 5615fc4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 37 deletions.
4 changes: 1 addition & 3 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,6 @@ func main() {
}

func run(ctx *cli.Context) {
utils.HandleInterrupt()
cfg := utils.MakeEthConfig(ClientIdentifier, nodeNameVersion, ctx)
ethereum, err := eth.New(cfg)
if err != nil {
Expand Down Expand Up @@ -527,10 +526,9 @@ func blockRecovery(ctx *cli.Context) {

func startEth(ctx *cli.Context, eth *eth.Ethereum) {
// Start Ethereum itself

utils.StartEthereum(eth)
am := eth.AccountManager()

am := eth.AccountManager()
account := ctx.GlobalString(utils.UnlockedAccountFlag.Name)
accounts := strings.Split(account, " ")
for i, account := range accounts {
Expand Down
50 changes: 16 additions & 34 deletions cmd/utils/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 5615fc4

Please sign in to comment.