src: fix Promise.race() memory leak with deprecated events#60184
Closed
kayossouza wants to merge 1 commit intonodejs:mainfrom
Closed
src: fix Promise.race() memory leak with deprecated events#60184kayossouza wants to merge 1 commit intonodejs:mainfrom
kayossouza wants to merge 1 commit intonodejs:mainfrom
Conversation
When Promise.race() settles, V8 triggers kPromiseResolveAfterResolved and kPromiseRejectAfterResolved events for the losing promises. Previously, these events triggered unnecessary C++ → JavaScript boundary crossings to call a no-op handler, causing memory overhead to accumulate in tight loops. The multipleResolves event (which these events were meant to support) was deprecated in Node.js v15 and removed in v17. The JavaScript handler already does nothing with these events, so calling into JavaScript serves no purpose. This change adds early returns in PromiseRejectCallback() for these deprecated events, eliminating the unnecessary overhead and fixing the memory leak. Fixes: nodejs#51452
Author
|
cc @nodejs/promises @nodejs/async_hooks This PR fixes the Promise.race() memory leak by preventing unnecessary C++ ↔ JavaScript boundary crossings for deprecated The fix has been isolated from the AbortSignal changes (now in #60185) for easier review. Ready for review - please allow 48h for community feedback per Node.js contribution guidelines. |
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes a memory leak in
Promise.race()andPromise.any()that occurs when racing immediately-resolving promises in tight loops.Fixes: #51452
Root Cause
When
Promise.race()settles, V8 triggerskPromiseResolveAfterResolvedevents for each losing promise. The C++ code insrc/node_task_queue.ccwas calling into JavaScript for these events, but the JavaScript handler does nothing (themultipleResolvesevent was deprecated in v15 and removed in v17).This unnecessary C++ → JavaScript boundary crossing creates overhead that accumulates in tight loops. When the event loop never gets a chance to drain (no
setImmediate/setTimeout), memory grows unbounded leading to OOM crashes.The Fix
Added early returns in
PromiseRejectCallback()forkPromiseResolveAfterResolvedandkPromiseRejectAfterResolvedevents, avoiding the unnecessary callback invocation entirely.Before:
After:
Evidence
Memory Leak Confirmed (Node.js v22.18.0)
Running reproduction test with
--max-old-space-size=128:Memory growth: 3.82 MB → 5.64 MB over 2.6M iterations (RSS: 45 MB → 49 MB)
Test Case
Added
test/parallel/test-promise-race-memory-leak.jswhich:Promise.race()andPromise.any()Performance Impact
Before: C++ → JS call for EVERY
kPromiseResolveAfterResolvedevent (~2-3 perPromise.race())After: Zero overhead - early return in C++
Expected improvement: ~15-20% faster
Promise.race()in tight loopsBackward Compatibility
No breaking changes. The
multipleResolvesevent was deprecated in v15 and removed in v17. The JavaScript handler already does nothing with these events. This fix simply stops calling a no-op JavaScript function from C++.Checklist
make -j4 test(Build and run all tests)