Skip to content

Commit

Permalink
Merge pull request #1780 from OffchainLabs/nitro_shutdown
Browse files Browse the repository at this point in the history
nitro: fix defers on main process
  • Loading branch information
PlasmaPower authored Jul 24, 2023
2 parents 184eac9 + a6893bd commit 46c5a5d
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions cmd/nitro/nitro.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,20 +374,26 @@ func mainImpl() int {
return 1
}

var deferFuncs []func()
defer func() {
for i := range deferFuncs {
deferFuncs[i]()
}
}()

chainDb, l2BlockChain, err := openInitializeChainDb(ctx, stack, nodeConfig, new(big.Int).SetUint64(nodeConfig.L2.ChainID), execution.DefaultCacheConfigFor(stack, &nodeConfig.Node.Caching), l1Client, rollupAddrs)
defer closeDb(chainDb, "chainDb")
if l2BlockChain != nil {
// Calling Stop on the blockchain multiple times does nothing
defer l2BlockChain.Stop()
deferFuncs = append(deferFuncs, func() { l2BlockChain.Stop() })
}
deferFuncs = append(deferFuncs, func() { closeDb(chainDb, "chainDb") })
if err != nil {
flag.Usage()
log.Error("error initializing database", "err", err)
return 1
}

arbDb, err := stack.OpenDatabase("arbitrumdata", 0, 0, "", false)
defer closeDb(arbDb, "arbDb")
deferFuncs = append(deferFuncs, func() { closeDb(arbDb, "arbDb") })
if err != nil {
log.Error("failed to open database", "err", err)
return 1
Expand Down Expand Up @@ -479,7 +485,8 @@ func mainImpl() int {
if err != nil {
fatalErrChan <- fmt.Errorf("error starting node: %w", err)
}
defer currentNode.StopAndWait()
// remove previous deferFuncs, StopAndWait closes database and blockchain.
deferFuncs = []func(){func() { currentNode.StopAndWait() }}
}

sigint := make(chan os.Signal, 1)
Expand Down

0 comments on commit 46c5a5d

Please sign in to comment.