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

async_hooks: eliminate native PromiseHook #39135

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 9 additions & 6 deletions lib/domain.js
Expand Up @@ -36,6 +36,7 @@ const {
Error,
FunctionPrototypeCall,
ObjectDefineProperty,
Promise,
ReflectApply,
SafeMap,
Symbol,
Expand Down Expand Up @@ -73,12 +74,14 @@ const asyncHook = createHook({
if (process.domain !== null && process.domain !== undefined) {
// If this operation is created while in a domain, let's mark it
pairing.set(asyncId, process.domain[kWeak]);
ObjectDefineProperty(resource, 'domain', {
configurable: true,
enumerable: false,
value: process.domain,
writable: true
});
if (type !== 'PROMISE' || resource instanceof Promise) {
Qard marked this conversation as resolved.
Show resolved Hide resolved
ObjectDefineProperty(resource, 'domain', {
configurable: true,
enumerable: false,
value: process.domain,
writable: true
});
}
}
},
before(asyncId) {
Expand Down
49 changes: 34 additions & 15 deletions lib/internal/async_hooks.js
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
1 change: 1 addition & 0 deletions lib/internal/trace_events_async_hooks.js
Expand Up @@ -31,6 +31,7 @@ const kEnabled = Symbol('enabled');
// twice the async_wrap.Providers list is used to filter the events.
const nativeProviders = new SafeSet(ObjectKeys(async_wrap.Providers));
const typeMemory = new SafeMap();
nativeProviders.delete('PROMISE');
Qard marked this conversation as resolved.
Show resolved Hide resolved

function createHook() {
// In traceEvents it is not only the id but also the name that needs to be
Expand Down