Flushed pathdb batches stay in the Go heap (pebble v1.1.5 pooled flush iterator), bor is OOM-killed during a long catch-up

Open
#2,561 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
30/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
go, linux

Research direction

Start in triedb/pathdb/buffer.go at (*buffer).flush and triedb/pathdb/config.go for maxBufferSize, then trace the large batch into ethdb/pebble/pebble.go and pebble's internal/arenaskl/flush_iterator.go / compaction.newInputIter path. Run the described large-batch reproduction or an equivalent heap-profile test and confirm flushed batch buffers are released promptly. Done means the reproduction's live flushed batches and peak heap drop without relying on --cache.gc 0, with the chosen fix documented for path-scheme nodes.

Written by the indexing model from the issue text.

Description

System

  • bor v2.11.0 (3a018e38), mainnet, full node on the path scheme: --syncmode full --gcmode full --cache 8192 --cache.triesinmemory 1024.
  • Linux amd64, NVMe, container memory limit 110 GiB, GOMEMLIMIT=100GiB.
  • pebble v1.1.5 (the version in go.mod of every release up to v2.11.2).

What happens

The node was about 25 hours behind and caught up in a bit over 5 hours.
Memory grew by about 4 GiB per pathdb flush and the kernel killed bor at the container limit twice: 52 minutes after the start (108 GiB) and again 2 h 46 min after the restart.
At the tip the same node sits at 35-36 GiB.

Heap profiles (debug_writeMemProfile) taken once a minute during the second run show the memory in triedb/pathdb.(*buffer).flush.func1 -> pebble.(*DB).NewBatchWithSize: 3-8 flush batches of 0.5-1.7 GB each still in use for the first 90 minutes, then 14-15 of them (23 GB).
Each of these batches had been committed long before and its flush to L0 had finished.

#2215 looks like the same problem (same heap signature on v2.7.3; it was closed as stale).

Why

  1. With --cache 8192 and the default --cache.gc 25, the pathdb write buffer is 2 GiB (maxBufferSize in triedb/pathdb/config.go allows 2048 MB).
    (*buffer).flush (triedb/pathdb/buffer.go) writes the nodes and all buffered states as one batch, db.NewBatchWithSize((flushNodes.dbsize() + flushStates.dbsize()) * 11 / 10).
    During a catch-up that is one batch of 0.5-2 GB every ~2 minutes.

  2. Pebble does not copy a batch whose accounted size is at least half a memtable (db.go: batch.memTableSize >= d.largeBatchThreshold).
    It queues the batch's own buffer as a "flushable batch" next to the memtables.
    The accounted size includes about 187 bytes per entry, so with bor's 512 MB memtable the 256 MB threshold is reached by roughly 85-150 MB of pathdb data.

  3. When the flush runs, compaction.newInputIter (compaction.go:1370) builds the memtable iterators with f.newFlushIter(nil, &c.bytesIterated).
    For a memtable this is an arenaskl.flushIterator:

    type flushIterator struct {
    	Iterator
    	bytesIterated *uint64
    }
    

    flushIterator has no Close of its own, so closing it calls the promoted (*Iterator).Close, which does iterPool.Put(it) (internal/arenaskl/iterator.go:57).
    The pooled pointer is an interior pointer into the flushIterator allocation, so the whole object stays alive, including bytesIterated.
    That field points into the compaction struct of the finished flush, and the struct's flushing slice still references every flushable of that flush, including the flushable batches and their buffers.

  4. Skiplist.NewIter reuses pool objects with *it = Iterator{...}, which resets only the embedded part and never clears bytesIterated.
    As long as reads keep the pool busy, the object and everything behind it survive.
    sync.Pool only drops objects that sat idle for two GC cycles, so the batches are released at random, minutes later.

  5. It feeds itself: pinned batches make the live heap larger, the GC target doubles with it, GC cycles become rarer, and pool entries live longer.

This was fixed in pebble by removing bytesIterated (cockroachdb/pebble c34894c467, 2024-04), which is only in v2.0.0 and later.

A heap dump of the reproduction below confirms the chain: arenaskl.iterPool -> flushIterator (112 bytes, allocated in (*memTable).newFlushIter from (*compaction).newInputIter) -> bytesIterated -> compaction -> flushing -> flushableEntry -> flushableBatch -> batch data.
I have no heap dump from the production node (it would stop the node for too long); the production evidence is the heap profile signature above.

Reproduction (pebble only, 2 minutes)

A small program opens pebble v1.1.5 with the options from ethdb/pebble/pebble.go (scaled down: 128 MB cache, so 16 MB memtables), commits one 30 MB batch every 3 s with NoSync the way (*buffer).flush does, and runs Get readers, short iterators and a small-batch writer next to it.
It tracks every batch buffer with a weak pointer and prints how many are still alive each second.

Build Batches Most flushed batches alive at once Peak HeapInuse
pebble v1.1.5 30 x 30 MB 15 944 MB
pebble v1.1.5 + patch below 30 x 30 MB 3 185 MB
pebble v1.1.5, batches below the threshold 300 x 3 MB 1 19 MB

The patch is one method in internal/arenaskl/flush_iterator.go:

// Close does not return the iterator to iterPool: the pooled object would
// keep bytesIterated, and through it the finished flush, reachable.
func (it *flushIterator) Close() error {
	*it = flushIterator{}
	return nil
}

I can attach the program (about 330 lines) and the heap-dump walker.

Workaround

--cache.gc 0.
The write buffer is gone, pathdb writes every block in batches of a few MB, pebble copies them into the memtable, and nothing large can be pinned.
(--cache.gc 1 is not enough at --cache 8192: the worst-case batch is close to the threshold.)

Possible fixes in bor

  • Write a pathdb flush as several batches below pebble's large-batch threshold instead of one.
  • Or move to pebble v2, or carry the one-method patch on v1.1.5.
  • Or change the default cache.gc for path-scheme nodes and document it; the packaged archive config already ships gc = 0.

go-ethereum pins the same pebble version, but its pathdb buffer is capped at 256 MB, so the effect there should be much smaller.

Dominant language
Go
Stars
1.1k
Forks
623
Avg merge
4d 14h
Merged PRs (30d)
34

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from 0xPolygon/bor

All issues in 0xPolygon/bor

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.