#35788·go-ethereum

triedb/pathdb: Disable leaks the disk layer's clean caches

Author: CPerezzCreated Sep 24, 2026Updated Sep 24, 2026

Context

We are implementing the MPT to binary trie (EIP-7864) state migration in a fork. Once the migration window closes, a full node deletes the frozen merkle state while it keeps running, so its live merkle pathdb has to be deactivated first: every layer marked stale, so readers taken earlier fail with errSnapshotStale instead of reading deleted records as empty accounts. That is Disable minus the snap-sync flag, so we built on it (https://github.com/CPerezz/go-ethereum/pull/39). Reviewing that change, we found that Disable never releases the disk layer's clean caches.

Finding

Disable marks the disk layer stale without releasing its clean caches:

go
disk := db.tree.bottom()
if err := disk.terminate(); err != nil {
	return err
}
disk.markStale()

After that nothing can release them:

  • resetCache returns early on a stale layer ("Stale disk layer loses the ownership of clean caches"), and Close only resets the current bottom layer.
  • Enable and AdoptSyncedState build the new disk layer with nil caches, so newDiskLayer allocates fresh ones.
  • fastcache allocates its chunks with mmap, outside the Go heap, and takes them back only through Reset. The old caches' memory stays allocated until the process exits.

The loss is whatever the caches hold when Disable runs; a stale layer rejects reads before touching them, so they don't grow afterwards. At startup they are empty, so a fresh snap sync loses almost nothing. It matters when snap sync is re-enabled on a running node (syncModer.get: head below the last pivot, or head state missing): each time, up to TrieCleanSize + StateCleanSize stays allocated.

Reproduce

On master (920c07774c), add to triedb/pathdb/database_test.go:

go
func TestDisableReleasesCleanCaches(t *testing.T) {
	maxDiffLayers = 4
	defer func() {
		maxDiffLayers = 128
	}()

	tester := newTester(t, &testerConfig{layers: 12})
	defer tester.release()

	// Flush everything to disk, which populates the clean caches.
	if err := tester.db.Commit(tester.lastHash(), false); err != nil {
		t.Fatalf("Failed to commit, err: %v", err)
	}
	disk := tester.db.tree.bottom()
	if n := cachedEntries(disk); n == 0 {
		t.Fatal("Clean caches are empty before disabling")
	}
	if err := tester.db.Disable(); err != nil {
		t.Fatalf("Failed to deactivate database: %v", err)
	}
	if n := cachedEntries(disk); n != 0 {
		t.Fatalf("Disabled disk layer still holds %d clean cache entries", n)
	}
}

func cachedEntries(dl *diskLayer) uint64 {
	var s fastcache.Stats
	dl.nodes.UpdateStats(&s)
	dl.states.UpdateStats(&s)
	return s.EntriesCount
}
$ go test ./triedb/pathdb -run TestDisableReleasesCleanCaches
--- FAIL: TestDisableReleasesCleanCaches (0.65s)
    database_test.go:807: Disabled disk layer still holds 1428 clean cache entries