Skip to content

Report wrapper cppgc allocations to V8 outside the no-GC scope - #7649

Merged
dcarney-cf merged 1 commit into
mainfrom
dcarney/cppgc-nogc-alloc-reporting
Oct 7, 2026
Merged

dcarney-cf merged 1 commit into
mainfrom
dcarney/cppgc-nogc-alloc-reporting

Conversation

@dcarney-cf

Copy link
Copy Markdown
Contributor

attachWrapper() made both of its cppgc allocations, the CppgcShim and the slot wrapper V8 allocates for SetAlignedPointerInInternalField(), inside a cppgc NoGarbageCollectionScope. cppgc reports allocations to V8 only when GC is allowed, and nothing reports the buffered bytes when the scope closes. A loop that creates short-lived API objects therefore never told V8 the cppgc heap was growing, so V8 ran only minor GCs.

Each wrapper takes two entries in V8's CppHeap pointer table, which only a major GC frees. Without one, the table filled after about 32M wrappers and the process aborted with "ExternalEntityTable:: AllocateEntry: allocation failed: process out of memory"

Do both allocations before entering the scope. The scope only needs to cover creating the TracedReference and linking the wrapper to its shim, and neither allocates on the cppgc heap.

attachWrapper() made both of its cppgc allocations, the CppgcShim and
the slot wrapper V8 allocates for SetAlignedPointerInInternalField(),
inside a cppgc NoGarbageCollectionScope. cppgc reports allocations to
V8 only when GC is allowed, and nothing reports the buffered bytes
when the scope closes. A loop that creates short-lived API objects
therefore never told V8 the cppgc heap was growing, so V8 ran only
minor GCs.

Each wrapper takes two entries in V8's CppHeap pointer table, which
only a major GC frees. Without one, the table filled after about 32M
wrappers and the process aborted with "ExternalEntityTable::
AllocateEntry: allocation failed: process out of memory". In
production this shows up in Durable Object alarms that call
sql.exec() in a loop, and in loops of `new TextEncoder()`.

Do both allocations before entering the scope. The scope only needs
to cover creating the TracedReference and linking the wrapper to its
shim, and neither allocates on the cppgc heap.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@dcarney-cf
dcarney-cf requested review from a team as code owners October 7, 2026 09:12
@ask-bonk

ask-bonk Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

LGTM!

Moves wrapper allocations outside the no-GC scope and adds a major-GC regression test.

Specialist findings not posted (1)
  • security#1 [WARNING] src/workerd/jsg/wrappable.c++:526: allocateShim() can now allocate while GC is permitted, but the new active shim is not yet reachable from the wrapper (that only happens at line 574) and its wrapper traced reference remains empty … — The claimed lifetime gap is not present: a newly created cppgc object is black-allocated for the active collection, and the source's protected window starts only at creation of the unrooted TracedRefe…

Reviewed commit: 6413f2b4 · github run

@floitsch floitsch left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@dcarney-cf
dcarney-cf merged commit 651ca60 into main Oct 7, 2026
41 of 43 checks passed
@dcarney-cf
dcarney-cf deleted the dcarney/cppgc-nogc-alloc-reporting branch October 7, 2026 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants