-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
src: skip JS callback for settled Promise.race losers #62336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -53,7 +53,11 @@ void PromiseRejectCallback(PromiseRejectMessage message) { | |||||||||
|
|
||||||||||
| Environment* env = Environment::GetCurrent(isolate); | ||||||||||
|
|
||||||||||
| if (env == nullptr || !env->can_call_into_js()) return; | ||||||||||
| if (env == nullptr || !env->can_call_into_js() || | ||||||||||
| event == kPromiseResolveAfterResolved || | ||||||||||
| event == kPromiseRejectAfterResolved) { | ||||||||||
|
Comment on lines
+57
to
+58
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we do this as
Suggested change
instead? That way this won't break when these are removed from V8 (which I am actively trying to do). It's equivalent because those are the only four kinds of event which exist. |
||||||||||
| return; | ||||||||||
| } | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: these can just be folded up into the early return if check immediately above and the comment likely isn't all that necessary |
||||||||||
|
|
||||||||||
| Local<Function> callback = env->promise_reject_callback(); | ||||||||||
| // The promise is rejected before JS land calls SetPromiseRejectCallback | ||||||||||
|
|
@@ -77,10 +81,6 @@ void PromiseRejectCallback(PromiseRejectMessage message) { | |||||||||
| "rejections", | ||||||||||
| "unhandled", unhandledRejections, | ||||||||||
| "handledAfter", rejectionsHandledAfter); | ||||||||||
| } else if (event == kPromiseResolveAfterResolved) { | ||||||||||
| value = message.GetValue(); | ||||||||||
| } else if (event == kPromiseRejectAfterResolved) { | ||||||||||
| value = message.GetValue(); | ||||||||||
| } else { | ||||||||||
| return; | ||||||||||
| } | ||||||||||
|
|
@@ -173,8 +173,6 @@ static void Initialize(Local<Object> target, | |||||||||
| Local<Object> events = Object::New(isolate); | ||||||||||
| NODE_DEFINE_CONSTANT(events, kPromiseRejectWithNoHandler); | ||||||||||
| NODE_DEFINE_CONSTANT(events, kPromiseHandlerAddedAfterReject); | ||||||||||
| NODE_DEFINE_CONSTANT(events, kPromiseResolveAfterResolved); | ||||||||||
| NODE_DEFINE_CONSTANT(events, kPromiseRejectAfterResolved); | ||||||||||
|
|
||||||||||
| target->Set(env->context(), | ||||||||||
| FIXED_ONE_BYTE_STRING(isolate, "promiseRejectEvents"), | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // Flags: --max-old-space-size=64 | ||
| 'use strict'; | ||
|
|
||
| // Regression test for https://github.com/nodejs/node/issues/51452 | ||
| // When Promise.race() settles, V8 fires kPromiseResolveAfterResolved / | ||
| // kPromiseRejectAfterResolved for each "losing" promise. Before this fix, | ||
| // the C++ PromiseRejectCallback crossed into JS for these no-op events, | ||
| // accumulating references and causing OOM in tight async loops. | ||
| // With --max-old-space-size=64, this test would crash before completing | ||
| // if the leak is present. | ||
|
|
||
| const common = require('../common'); | ||
|
|
||
| async function main() { | ||
| for (let i = 0; i < 100_000; i++) { | ||
| await Promise.race([ | ||
| Promise.resolve(1), | ||
| Promise.resolve(2), | ||
| Promise.resolve(3), | ||
| ]); | ||
| } | ||
| } | ||
|
|
||
| main().then(common.mustCall()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a harm from keeping the cases and panicking if they’re hit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kPromiseRejectAfterResolvedandkPromiseResolveAfterResolvedare no longer even defined on the JS side after this patch.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They also never worked
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @bakkot @benjamingr, that covers it!