Skip to content

Commit

Permalink
Ensure the miner is closed
Browse files Browse the repository at this point in the history
Also make miner and worker wait for goroutines to end at Shutdown.

Without this change, when shutting down its possible for the worker to
try and access the db after it has been closed, which results in a
critical failure and runs the risk of corrupting the db.
  • Loading branch information
piersy committed Oct 1, 2021
1 parent b522f5e commit 082ec8f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions eth/backend.go
Expand Up @@ -555,6 +555,7 @@ func (s *Ethereum) Stop() error {
close(s.closeBloomHandler)
s.txPool.Stop()
s.miner.Stop()
s.miner.Close()
s.blockchain.Stop()
s.engine.Close()
rawdb.PopUncleanShutdownMarker(s.chainDb)
Expand Down
4 changes: 3 additions & 1 deletion miner/miner.go
Expand Up @@ -139,6 +139,7 @@ func (miner *Miner) update() {
miner.worker.stop()
case <-miner.exitCh:
miner.worker.close()
miner.exitCh <- struct{}{}
return
}
}
Expand All @@ -153,7 +154,8 @@ func (miner *Miner) Stop() {
}

func (miner *Miner) Close() {
close(miner.exitCh)
miner.exitCh <- struct{}{}
<-miner.exitCh
}

func (miner *Miner) Mining() bool {
Expand Down
23 changes: 19 additions & 4 deletions miner/worker.go
Expand Up @@ -147,6 +147,7 @@ type worker struct {
resultCh chan *types.Block
startCh chan struct{}
exitCh chan struct{}
wg sync.WaitGroup
resubmitIntervalCh chan time.Duration
resubmitAdjustCh chan *intervalAdjust

Expand Down Expand Up @@ -225,10 +226,23 @@ func newWorker(config *Config, chainConfig *params.ChainConfig, engine consensus
recommit = minRecommitInterval
}

go worker.mainLoop()
go worker.newWorkLoop(recommit)
go worker.resultLoop()
go worker.taskLoop()
worker.wg.Add(4)
go func() {
defer worker.wg.Done()
worker.mainLoop()
}()
go func() {
defer worker.wg.Done()
worker.newWorkLoop(recommit)
}()
go func() {
defer worker.wg.Done()
worker.resultLoop()
}()
go func() {
defer worker.wg.Done()
worker.taskLoop()
}()

// Submit first work to initialize pending state.
if init {
Expand Down Expand Up @@ -323,6 +337,7 @@ func (w *worker) close() {
}
atomic.StoreInt32(&w.running, 0)
close(w.exitCh)
w.wg.Wait()
}

// recalcRecommit recalculates the resubmitting interval upon feedback.
Expand Down

0 comments on commit 082ec8f

Please sign in to comment.