Repository navigation
Fix deferred external memory decreases in C++ JSG - #8497
Conversation
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
✅ 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)
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().