From 8219bfcaddb79ada704713d216ae54b1f7fb13f2 Mon Sep 17 00:00:00 2001 From: Delweng Date: Thu, 19 Jun 2025 09:44:39 +0800 Subject: [PATCH] eth,core: terminate the downloader immediately when shutdown signal is received (#32062) Closes https://github.com/ethereum/go-ethereum/issues/32058 --- core/blockchain.go | 7 ++----- eth/downloader/downloader.go | 6 ++++++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 553be35e53..2794513025 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -278,9 +278,8 @@ type BlockChain struct { txLookupLock sync.RWMutex txLookupCache *lru.Cache[common.Hash, txLookup] - quit chan struct{} // shutdown signal, closed in Stop. - stopping atomic.Bool // false if chain is running, true when stopped - procInterrupt atomic.Bool // interrupt signaler for block processing + stopping atomic.Bool // false if chain is running, true when stopped + procInterrupt atomic.Bool // interrupt signaler for block processing engine consensus.Engine validator Validator // Block and state validator interface @@ -328,7 +327,6 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis db: db, triedb: triedb, triegc: prque.New[int64, common.Hash](nil), - quit: make(chan struct{}), chainmu: syncx.NewClosableMutex(), bodyCache: lru.NewCache[common.Hash, *types.Body](bodyCacheLimit), bodyRLPCache: lru.NewCache[common.Hash, rlp.RawValue](bodyCacheLimit), @@ -1206,7 +1204,6 @@ func (bc *BlockChain) stopWithoutSaving() { bc.scope.Close() // Signal shutdown to all goroutines. - close(bc.quit) bc.StopInsert() // Now wait for all chain modifications to end and persistent goroutines to exit. diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index 762fb9283e..8f8f219597 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -199,6 +199,9 @@ type BlockChain interface { // InsertChain inserts a batch of blocks into the local chain. InsertChain(types.Blocks) (int, error) + // StopInsert interrupts the inserting process. + StopInsert() + // InsertReceiptChain inserts a batch of blocks along with their receipts // into the local chain. Blocks older than the specified `ancientLimit` // are stored directly in the ancient store, while newer blocks are stored @@ -634,6 +637,9 @@ func (d *Downloader) Cancel() { // Terminate interrupts the downloader, canceling all pending operations. // The downloader cannot be reused after calling Terminate. func (d *Downloader) Terminate() { + // Signal to stop inserting in-flight blocks + d.blockchain.StopInsert() + // Close the termination channel (make sure double close is allowed) d.quitLock.Lock() select {