Skip to content
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

process: improve rejections tracking performance #34862

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 26 additions & 27 deletions lib/internal/process/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
Error,
ObjectDefineProperty,
WeakMap,
Map,
} = primordials;

const {
Expand All @@ -26,7 +27,7 @@ const {
const kHasRejectionToWarn = 1;

const maybeUnhandledPromises = new WeakMap();
const pendingUnhandledRejections = [];
let pendingUnhandledRejections = new Map();
const asyncHandledRejections = [];
let lastPromiseId = 0;

Expand Down Expand Up @@ -119,32 +120,33 @@ function resolveError(type, promise, reason) {
}

function unhandledRejection(promise, reason) {
maybeUnhandledPromises.set(promise, {
const uid = ++lastPromiseId;
// This causes the promise to be referenced at least for one tick.
pendingUnhandledRejections.set(promise, {
reason,
uid: ++lastPromiseId,
warned: false
uid
});
// This causes the promise to be referenced at least for one tick.
pendingUnhandledRejections.push(promise);
setHasRejectionToWarn(true);
}

function handledRejection(promise) {
if (pendingUnhandledRejections.has(promise)) {
pendingUnhandledRejections.delete(promise);
return;
}
const promiseInfo = maybeUnhandledPromises.get(promise);
if (promiseInfo !== undefined) {
maybeUnhandledPromises.delete(promise);
if (promiseInfo.warned) {
const { uid } = promiseInfo;
// Generate the warning object early to get a good stack trace.
// eslint-disable-next-line no-restricted-syntax
const warning = new Error('Promise rejection was handled ' +
`asynchronously (rejection id: ${uid})`);
warning.name = 'PromiseRejectionHandledWarning';
warning.id = uid;
asyncHandledRejections.push({ promise, warning });
setHasRejectionToWarn(true);
return;
}
const { uid } = promiseInfo;
// Generate the warning object early to get a good stack trace.
// eslint-disable-next-line no-restricted-syntax
const warning = new Error('Promise rejection was handled ' +
`asynchronously (rejection id: ${uid})`);
warning.name = 'PromiseRejectionHandledWarning';
warning.id = uid;
asyncHandledRejections.push({ promise, warning });
setHasRejectionToWarn(true);
return;
}
if (maybeUnhandledPromises.size === 0 && asyncHandledRejections.length === 0)
setHasRejectionToWarn(false);
Expand Down Expand Up @@ -197,14 +199,11 @@ function processPromiseRejections() {
}
}

let len = pendingUnhandledRejections.length;
while (len--) {
const promise = pendingUnhandledRejections.shift();
const promiseInfo = maybeUnhandledPromises.get(promise);
if (promiseInfo === undefined) {
continue;
}
promiseInfo.warned = true;
const pending = pendingUnhandledRejections;
pendingUnhandledRejections = new Map();
for (const [promise, promiseInfo] of pending.entries()) {
pending.delete(promise);
maybeUnhandledPromises.set(promise, promiseInfo);
const { reason, uid } = promiseInfo;
switch (unhandledRejectionsMode) {
case kStrictUnhandledRejections: {
Expand Down Expand Up @@ -256,7 +255,7 @@ function processPromiseRejections() {
maybeScheduledTicksOrMicrotasks = true;
}
return maybeScheduledTicksOrMicrotasks ||
pendingUnhandledRejections.length !== 0;
pendingUnhandledRejections.size !== 0;
}

function getErrorWithoutStack(name, message) {
Expand Down