Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core/state, tests: fix memory leak via fastcache #28387

Merged
merged 3 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
core/state, tests: fix memory leak via fastcache
  • Loading branch information
holiman committed Oct 20, 2023
commit 07bf799be52532379fed054115d9f7a14fb4fba8
1 change: 1 addition & 0 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,7 @@ func (bc *BlockChain) Stop() {
if snapBase, err = bc.snaps.Journal(bc.CurrentBlock().Root); err != nil {
log.Error("Failed to journal state snapshot", "err", err)
}
bc.snaps.Release()
}
if bc.triedb.Scheme() == rawdb.PathScheme {
// Ensure that the in-memory trie nodes are journaled to disk properly.
Expand Down
11 changes: 11 additions & 0 deletions core/state/snapshot/disklayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ type diskLayer struct {
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.
func (dl *diskLayer) Root() common.Hash {
return dl.root
Expand Down
7 changes: 7 additions & 0 deletions core/state/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,13 @@ func diffToDisk(bottom *diffLayer) *diskLayer {
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.
// This is meant to be used during shutdown to persist the snapshot without
// flattening everything down (bad for reorgs).
Expand Down
3 changes: 3 additions & 0 deletions tests/state_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ func (t *StateTest) Run(subtest StateSubtest, vmconfig vm.Config, snapshotter bo
if triedb != nil {
triedb.Close()
}
if snaps != nil {
snaps.Release()
}
}()
checkedErr := t.checkError(subtest, err)
if checkedErr != nil {
Expand Down