eth: close miner on exit (instead of just stopping) (#21992)
This ensures that all miner goroutines have exited before stopping the blockchain. Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
committed by
GitHub
parent
2fe0c65f4b
commit
28d30b51f8
@ -20,6 +20,7 @@ package miner
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
@ -63,6 +64,8 @@ type Miner struct {
|
||||
exitCh chan struct{}
|
||||
startCh chan common.Address
|
||||
stopCh chan struct{}
|
||||
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
func New(eth Backend, config *Config, chainConfig *params.ChainConfig, mux *event.TypeMux, engine consensus.Engine, isLocalBlock func(block *types.Block) bool) *Miner {
|
||||
@ -75,8 +78,8 @@ func New(eth Backend, config *Config, chainConfig *params.ChainConfig, mux *even
|
||||
stopCh: make(chan struct{}),
|
||||
worker: newWorker(config, chainConfig, engine, eth, mux, isLocalBlock, true),
|
||||
}
|
||||
miner.wg.Add(1)
|
||||
go miner.update()
|
||||
|
||||
return miner
|
||||
}
|
||||
|
||||
@ -85,6 +88,8 @@ func New(eth Backend, config *Config, chainConfig *params.ChainConfig, mux *even
|
||||
// the loop is exited. This to prevent a major security vuln where external parties can DOS you with blocks
|
||||
// and halt your mining operation for as long as the DOS continues.
|
||||
func (miner *Miner) update() {
|
||||
defer miner.wg.Done()
|
||||
|
||||
events := miner.mux.Subscribe(downloader.StartEvent{}, downloader.DoneEvent{}, downloader.FailedEvent{})
|
||||
defer func() {
|
||||
if !events.Closed() {
|
||||
@ -154,6 +159,7 @@ func (miner *Miner) Stop() {
|
||||
|
||||
func (miner *Miner) Close() {
|
||||
close(miner.exitCh)
|
||||
miner.wg.Wait()
|
||||
}
|
||||
|
||||
func (miner *Miner) Mining() bool {
|
||||
|
Reference in New Issue
Block a user