core/state, tests: fix memory leak via fastcache

This commit is contained in:
Martin Holst Swende 2023-10-20 10:04:58 +02:00
parent cd29535672
commit 07bf799be5
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
4 changed files with 22 additions and 0 deletions

View file

@ -989,6 +989,7 @@ func (bc *BlockChain) Stop() {
if snapBase, err = bc.snaps.Journal(bc.CurrentBlock().Root); err != nil { if snapBase, err = bc.snaps.Journal(bc.CurrentBlock().Root); err != nil {
log.Error("Failed to journal state snapshot", "err", err) log.Error("Failed to journal state snapshot", "err", err)
} }
bc.snaps.Release()
} }
if bc.triedb.Scheme() == rawdb.PathScheme { if bc.triedb.Scheme() == rawdb.PathScheme {
// Ensure that the in-memory trie nodes are journaled to disk properly. // Ensure that the in-memory trie nodes are journaled to disk properly.

View file

@ -45,6 +45,17 @@ type diskLayer struct {
lock sync.RWMutex lock sync.RWMutex
} }
// Release releases underlying resources; specifically the fastcache requires
// Reset() in order to not leak memory.
// OBS: It does not invoke Close on the diskdb
func (dl *diskLayer) Release() error {
if dl.cache != nil {
dl.cache.Reset()
dl.cache = nil
}
return nil
}
// Root returns root hash for which this snapshot was made. // Root returns root hash for which this snapshot was made.
func (dl *diskLayer) Root() common.Hash { func (dl *diskLayer) Root() common.Hash {
return dl.root return dl.root

View file

@ -656,6 +656,13 @@ func diffToDisk(bottom *diffLayer) *diskLayer {
return res return res
} }
// Release releases resources
func (t *Tree) Release() {
if dl := t.disklayer(); dl != nil {
dl.Release()
}
}
// Journal commits an entire diff hierarchy to disk into a single journal entry. // Journal commits an entire diff hierarchy to disk into a single journal entry.
// This is meant to be used during shutdown to persist the snapshot without // This is meant to be used during shutdown to persist the snapshot without
// flattening everything down (bad for reorgs). // flattening everything down (bad for reorgs).

View file

@ -200,6 +200,9 @@ func (t *StateTest) Run(subtest StateSubtest, vmconfig vm.Config, snapshotter bo
if triedb != nil { if triedb != nil {
triedb.Close() triedb.Close()
} }
if snaps != nil {
snaps.Release()
}
}() }()
checkedErr := t.checkError(subtest, err) checkedErr := t.checkError(subtest, err)
if checkedErr != nil { if checkedErr != nil {