From c1d5a012ea4b824e902db14e47bf147d727c2657 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Fri, 20 Oct 2023 13:35:49 +0200 Subject: [PATCH] core/state, tests: fix memory leak via fastcache (#28387) This change fixes a memory leak, when running either state-tests or blockchain-tests, we allocate a `1MB` fastcache during snapshot generation. `fastcache` is a bit special, and requires a `Reset()` (it has it's own memory allocator). The `1MB` was hidden [here](https://github.com/ethereum/go-ethereum/blob/master/tests/state_test_util.go#L333) and [here](https://github.com/ethereum/go-ethereum/blob/master/tests/block_test_util.go#L146) respectively. --- appveyor.yml | 2 +- build/ci.go | 4 ++++ core/blockchain.go | 1 + core/state/snapshot/disklayer.go | 10 ++++++++++ core/state/snapshot/snapshot.go | 7 +++++++ tests/state_test_util.go | 3 +++ 6 files changed, 26 insertions(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index 114aec644..4a8c4b737 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -54,4 +54,4 @@ for: - go run build/ci.go archive -arch %GETH_ARCH% -type zip -signer WINDOWS_SIGNING_KEY -upload gethstore/builds - go run build/ci.go nsis -arch %GETH_ARCH% -signer WINDOWS_SIGNING_KEY -upload gethstore/builds test_script: - - go run build/ci.go test -dlgo -arch %GETH_ARCH% -cc %GETH_CC% + - go run build/ci.go test -dlgo -arch %GETH_ARCH% -cc %GETH_CC% -short diff --git a/build/ci.go b/build/ci.go index 1ff3fb5bf..46f1ac281 100644 --- a/build/ci.go +++ b/build/ci.go @@ -285,6 +285,7 @@ func doTest(cmdline []string) { coverage = flag.Bool("coverage", false, "Whether to record code coverage") verbose = flag.Bool("v", false, "Whether to log verbosely") race = flag.Bool("race", false, "Execute the race detector") + short = flag.Bool("short", false, "Pass the 'short'-flag to go test") cachedir = flag.String("cachedir", "./build/cache", "directory for caching downloads") ) flag.CommandLine.Parse(cmdline) @@ -318,6 +319,9 @@ func doTest(cmdline []string) { if *race { gotest.Args = append(gotest.Args, "-race") } + if *short { + gotest.Args = append(gotest.Args, "-short") + } packages := []string{"./..."} if len(flag.CommandLine.Args()) > 0 { diff --git a/core/blockchain.go b/core/blockchain.go index baf2f9f82..fc5c249ce 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -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. diff --git a/core/state/snapshot/disklayer.go b/core/state/snapshot/disklayer.go index 513f0f5ab..d563b67ca 100644 --- a/core/state/snapshot/disklayer.go +++ b/core/state/snapshot/disklayer.go @@ -45,6 +45,16 @@ 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() + } + return nil +} + // Root returns root hash for which this snapshot was made. func (dl *diskLayer) Root() common.Hash { return dl.root diff --git a/core/state/snapshot/snapshot.go b/core/state/snapshot/snapshot.go index e30a0005c..638984238 100644 --- a/core/state/snapshot/snapshot.go +++ b/core/state/snapshot/snapshot.go @@ -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). diff --git a/tests/state_test_util.go b/tests/state_test_util.go index 8c255c1b5..745a3c6b2 100644 --- a/tests/state_test_util.go +++ b/tests/state_test_util.go @@ -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 {