Skip to content

Fix deferred external memory decreases in C++ JSG - #8497

Merged
bartlomieju merged 3 commits into
mainfrom
biwanczuk/external-memory-netting
Nov 17, 2026
Merged

bartlomieju merged 3 commits into
mainfrom
biwanczuk/external-memory-netting

Conversation

@bartlomieju

@bartlomieju bartlomieju commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

An ExternalMemoryAdjustment created without the isolate lock records its
increase in a pending total. If it is moved to the lock holder and lowered
or destroyed before the next lock, V8 receives a decrease before seeing
the increase. Heap::UpdateExternalMemory checks that the total stays
nonnegative in release builds too, so this can abort the process.

Net a pending rise into a decrease reported under the lock. Leave any
remaining rise pending rather than sending V8 an increase from a path
that can run in a destructor or cppgc finalizer. Leave pending decreases
for the next lock so the immediate delta stays within the checked bound.

Route adjustNow() through the deferred path when the supplied lock is
for another isolate, including when the target isolate is destroyed.
Remove the debug assertion that rejected this case and document the
reporting rules for adjustNow() and setNow().

An ExternalMemoryAdjustment created without the isolate lock records its
increase in a pending total. If it is moved to the lock holder and lowered
or destroyed before the next lock, V8 receives a decrease before seeing
the increase. Heap::UpdateExternalMemory checks that the total stays
nonnegative in release builds too, so this can abort the process.

Net a pending rise into a decrease reported under the lock. Leave any
remaining rise pending rather than sending V8 an increase from a path
that can run in a destructor or cppgc finalizer. Leave pending decreases
for the next lock so the immediate delta stays within the checked bound.

Route adjustNow() through the deferred path when the supplied lock is
for another isolate, including when the target isolate is destroyed.
Remove the debug assertion that rejected this case and document the
reporting rules for adjustNow() and setNow().

Add C++ regressions for destruction and explicit decreases of pending
increases, both signs of pending totals, and adjustments belonging to
another live or destroyed isolate.

Extracted from #8411 so the C++ accounting fix can land independently of
the Rust external memory API. No JavaScript API surface changes.

Validation: all 66 C++ JSG test targets pass; clang-tidy passes for
jsg-core and jsg-test_binary on the remote Linux toolchain.
@bartlomieju
bartlomieju requested review from a team as code owners November 16, 2026 16:39

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 495ffb78.

This review, of 0436e900

✅ Approved — No findings: the netting in decreaseNow() keeps V8's external memory total nonnegative and within the per-call bound, and the new adjustNow() handling for another or a destroyed isolate is correct.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security concerns in this change.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md updates needed.


📋 Code Quality: ✅ LGTM

Netting a pending rise into the decrease is sound. The value V8 receives always lies between amount and 0, so V8's total stays nonnegative and the call stays within the bound checkReasonableExternalMemoryDelta() checks. If the pending rise exceeds the decrease, the net rise goes back into the pending total. A pending fall stays deferred until the next lock. Both adjustNow() fallbacks (another live isolate, or a destroyed one) send the adjustment to the pending total, and the new tests match that behaviour.


⚡ Performance: ✅ LGTM

decreaseNow() adds one relaxed atomic load to each decrease. It does an exchange and a fetch_add only when a rise is pending. Each of these is O(1) and lock-free, with no allocation. The change never sends V8 an increase from a destructor or finalizer, so it cannot start a GC on that path.


📝 Documentation: ✅ LGTM

The comments in jsg.h and jsg.c++ describe the netting and deferral behaviour accurately.


Reviewed commit: 0436e900

@bartlomieju
bartlomieju marked this pull request as draft November 16, 2026 16:43
@bartlomieju
bartlomieju marked this pull request as ready for review November 17, 2026 12:15

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved — No findings: the netting in decreaseNow() keeps V8's external memory total nonnegative and within the per-call bound, and the new adjustNow() handling for another or a destroyed isolate is correct.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security concerns in this change.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md updates needed.


📋 Code Quality: ✅ LGTM

Netting a pending rise into the decrease is sound. The value V8 receives always lies between amount and 0, so V8's total stays nonnegative and the call stays within the bound checkReasonableExternalMemoryDelta() checks. If the pending rise exceeds the decrease, the net rise goes back into the pending total. A pending fall stays deferred until the next lock. Both adjustNow() fallbacks (another live isolate, or a destroyed one) send the adjustment to the pending total, and the new tests match that behaviour.


⚡ Performance: ✅ LGTM

decreaseNow() adds one relaxed atomic load to each decrease. It does an exchange and a fetch_add only when a rise is pending. Each of these is O(1) and lock-free, with no allocation. The change never sends V8 an increase from a destructor or finalizer, so it cannot start a GC on that path.


📝 Documentation: ✅ LGTM

The comments in jsg.h and jsg.c++ describe the netting and deferral behaviour accurately.


Reviewed commit: 495ffb78 — same changes as 0436e900, so this is that commit's review (no new review was run; line numbers are as of 0436e900)

@bartlomieju
bartlomieju merged commit 2a07e8e into main Nov 17, 2026
49 of 52 checks passed
@bartlomieju
bartlomieju deleted the biwanczuk/external-memory-netting branch November 17, 2026 14:21
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.

2 participants