Skip to content

Commit ef227c5

Browse files
committed
core: fix temp memory blowup caused by defers holding on to state
1 parent 111abdc commit ef227c5

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

core/blockchain.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -1805,6 +1805,17 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, er
18051805
return it.index, err
18061806
}
18071807
// No validation errors for the first block (or chain prefix skipped)
1808+
var activeState *state.StateDB
1809+
defer func() {
1810+
// The chain importer is starting and stopping trie prefetchers. If a bad
1811+
// block or other error is hit however, an early return may not properly
1812+
// terminate the background threads. This defer ensures that we clean up
1813+
// and dangling prefetcher, without defering each and holding on live refs.
1814+
if activeState != nil {
1815+
activeState.StopPrefetcher()
1816+
}
1817+
}()
1818+
18081819
for ; block != nil && err == nil || err == ErrKnownBlock; block, err = it.next() {
18091820
// If the chain is terminating, stop processing blocks
18101821
if bc.insertStopped() {
@@ -1867,7 +1878,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, er
18671878
}
18681879
// Enable prefetching to pull in trie node paths while processing transactions
18691880
statedb.StartPrefetcher("chain")
1870-
defer statedb.StopPrefetcher() // stopped on write anyway, defer meant to catch early error returns
1881+
activeState = statedb
18711882

18721883
// If we have a followup block, run that against the current state to pre-cache
18731884
// transactions and probabilistically some of the account/storage trie nodes.

0 commit comments

Comments
 (0)