Flushed pathdb batches stay in the Go heap (pebble v1.1.5 pooled flush iterator), bor is OOM-killed during a long catch-up
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
- Domain
- database, performance
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.modof 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
-
With
--cache 8192and the default--cache.gc 25, the pathdb write buffer is 2 GiB (maxBufferSizeintriedb/pathdb/config.goallows 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. -
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. -
When the flush runs,
compaction.newInputIter(compaction.go:1370) builds the memtable iterators withf.newFlushIter(nil, &c.bytesIterated).
For a memtable this is anarenaskl.flushIterator:type flushIterator struct { Iterator bytesIterated *uint64 }flushIteratorhas noCloseof its own, so closing it calls the promoted(*Iterator).Close, which doesiterPool.Put(it)(internal/arenaskl/iterator.go:57).
The pooled pointer is an interior pointer into theflushIteratorallocation, so the whole object stays alive, includingbytesIterated.
That field points into thecompactionstruct of the finished flush, and the struct'sflushingslice still references every flushable of that flush, including the flushable batches and their buffers. -
Skiplist.NewIterreuses pool objects with*it = Iterator{...}, which resets only the embedded part and never clearsbytesIterated.
As long as reads keep the pool busy, the object and everything behind it survive.
sync.Poolonly drops objects that sat idle for two GC cycles, so the batches are released at random, minutes later. -
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.gcfor path-scheme nodes and document it; the packaged archive config already shipsgc = 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
- Ships a Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from 0xPolygon/bor
-
Difficulty 4/5 3-5 days Newbie friendliness 22/100
0xPolygon/bor#2568 · 1 comment ·
Maintainers usually reply within 2 days
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
0xPolygon/bor#2539 · 1 comment ·
Maintainers usually reply within 2 days
-
Stale
Difficulty 4/5 3-5 days Newbie friendliness 35/100
0xPolygon/bor#2528 · 2 comments ·
Maintainers usually reply within 2 days
-
Backport go-ethereum#34898 so bor no longer deadlocks in p2p/discover waitForNodes and hangs on shutdownMay be free again A pull request for this issue was closed without being merged. Open
Difficulty 3/5 1-2 days Newbie friendliness 35/100
0xPolygon/bor#2506 · 4 comments · 1 reaction ·
Maintainers usually reply within 2 days
-
Snap sync on Amoy cannot progress past block 34,546,977: receipts exceed the 10 MiB eth maxMessageSize, every peer is disconnectedMay be free again @Samyra312007 claimed this 49 days ago, and no pull request is open. OpenStale
Difficulty 4/5 3-5 days Newbie friendliness 30/100
0xPolygon/bor#2441 · 5 comments ·
Maintainers usually reply within 2 days
Similar issues
-
needs-triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
gke-labs/kube-agents#7441 ·
Maintainers usually reply within 1 day
-
triage/pending-triage
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
external-secrets/external-secrets#7361 ·
Maintainers usually reply within 2 days
-
Type/Bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
thunder-id/thunderid#7092 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
jdx/jactionlint#360 ·
Maintainers usually reply within 1 day
-
enhancement good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Maintainers usually reply within 1 day