eth: interrupt chain insertion on shutdown (#21114)

This adds a new API method on core.BlockChain to allow interrupting
running data inserts, and calls the method before shutting down the
downloader.

The BlockChain interrupt checks are now done through a method instead
of inlining the atomic load everywhere. There is no loss of efficiency from
this and it makes the interrupt protocol a lot clearer because the check is
defined next to the method that sets the flag.
This commit is contained in:
Felix Lange
2020-05-26 21:37:37 +02:00
committed by GitHub
parent 4873a9d3c3
commit 9219e0fba4
3 changed files with 38 additions and 22 deletions

View File

@ -199,7 +199,6 @@ func (cs *chainSyncer) loop() {
cs.pm.txFetcher.Start()
defer cs.pm.blockFetcher.Stop()
defer cs.pm.txFetcher.Stop()
defer cs.pm.downloader.Terminate()
// The force timer lowers the peer count threshold down to one when it fires.
// This ensures we'll always start sync even if there aren't enough peers.
@ -222,8 +221,13 @@ func (cs *chainSyncer) loop() {
cs.forced = true
case <-cs.pm.quitSync:
// Disable all insertion on the blockchain. This needs to happen before
// terminating the downloader because the downloader waits for blockchain
// inserts, and these can take a long time to finish.
cs.pm.blockchain.StopInsert()
cs.pm.downloader.Terminate()
if cs.doneCh != nil {
cs.pm.downloader.Terminate() // Double term is fine, Cancel would block until queue is emptied
// Wait for the current sync to end.
<-cs.doneCh
}
return