Skip to content

Commit

Permalink
async_hooks: eliminate native PromiseHook
Browse files Browse the repository at this point in the history
PR-URL: nodejs#39135
Backport-PR-URL: nodejs#39742
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Gerhard Stöbich <[email protected]>
  • Loading branch information
Stephen Belanger authored and foxxyz committed Oct 18, 2021
1 parent 8e21bfd commit 5a1841c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 453 deletions.
49 changes: 34 additions & 15 deletions lib/internal/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const {
clearAsyncIdStack,
} = async_wrap;
// For performance reasons, only track Promises when a hook is enabled.
const { enablePromiseHook, disablePromiseHook, setPromiseHooks } = async_wrap;
const { setPromiseHooks } = async_wrap;
// Properties in active_hooks are used to keep track of the set of hooks being
// executed in case another hook is enabled/disabled. The new set of hooks is
// then restored once the active set of hooks is finished executing.
Expand Down Expand Up @@ -307,9 +307,13 @@ function trackPromise(promise, parent) {
return;
}

promise[async_id_symbol] = newAsyncId();
promise[trigger_async_id_symbol] = parent ? getOrSetAsyncId(parent) :
// Get trigger id from parent async id before making the async id of the
// child so if a new one must be made it will be lower than the child.
const triggerAsyncId = parent ? getOrSetAsyncId(parent) :
getDefaultTriggerAsyncId();

promise[async_id_symbol] = newAsyncId();
promise[trigger_async_id_symbol] = triggerAsyncId;
}

function promiseInitHook(promise, parent) {
Expand All @@ -319,6 +323,21 @@ function promiseInitHook(promise, parent) {
emitInitScript(asyncId, 'PROMISE', triggerAsyncId, promise);
}

function promiseInitHookWithDestroyTracking(promise, parent) {
promiseInitHook(promise, parent);
destroyTracking(promise, parent);
}

const destroyedSymbol = Symbol('destroyed');

function destroyTracking(promise, parent) {
trackPromise(promise, parent);
const asyncId = promise[async_id_symbol];
const destroyed = { destroyed: false };
promise[destroyedSymbol] = destroyed;
registerDestroyHook(promise, asyncId, destroyed);
}

function promiseBeforeHook(promise) {
trackPromise(promise);
const asyncId = promise[async_id_symbol];
Expand Down Expand Up @@ -355,18 +374,19 @@ function enableHooks() {

function updatePromiseHookMode() {
wantPromiseHook = true;
if (destroyHooksExist()) {
enablePromiseHook();
setPromiseHooks(undefined, undefined, undefined, undefined);
} else {
disablePromiseHook();
setPromiseHooks(
initHooksExist() ? promiseInitHook : undefined,
promiseBeforeHook,
promiseAfterHook,
promiseResolveHooksExist() ? promiseResolveHook : undefined,
);
let initHook;
if (initHooksExist()) {
initHook = destroyHooksExist() ? promiseInitHookWithDestroyTracking :
promiseInitHook;
} else if (destroyHooksExist()) {
initHook = destroyTracking;
}
setPromiseHooks(
initHook,
promiseBeforeHook,
promiseAfterHook,
promiseResolveHooksExist() ? promiseResolveHook : undefined,
);
}

function disableHooks() {
Expand All @@ -381,7 +401,6 @@ function disableHooks() {

function disablePromiseHookIfNecessary() {
if (!wantPromiseHook) {
disablePromiseHook();
setPromiseHooks(undefined, undefined, undefined, undefined);
}
}
Expand Down
Loading

0 comments on commit 5a1841c

Please sign in to comment.