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

inspector: skip promise hook in the inspector async hook #57148

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
7 changes: 5 additions & 2 deletions lib/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const AsyncContextFrame = require('internal/async_context_frame');
// Get functions
// For userland AsyncResources, make sure to emit a destroy event when the
// resource gets gced.
const { registerDestroyHook } = internal_async_hooks;
const { registerDestroyHook, kNoPromiseHook } = internal_async_hooks;
const {
asyncWrap,
executionAsyncId,
Expand Down Expand Up @@ -90,6 +90,7 @@ class AsyncHook {
this[after_symbol] = after;
this[destroy_symbol] = destroy;
this[promise_resolve_symbol] = promiseResolve;
this[kNoPromiseHook] = false;
}

enable() {
Expand Down Expand Up @@ -120,7 +121,9 @@ class AsyncHook {
enableHooks();
}

updatePromiseHookMode();
if (!this[kNoPromiseHook]) {
updatePromiseHookMode();
}

return this;
}
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ function triggerAsyncId() {
return async_id_fields[kTriggerAsyncId];
}

const kNoPromiseHook = Symbol('kNoPromiseHook');

module.exports = {
executionAsyncId,
Expand Down Expand Up @@ -624,4 +625,5 @@ module.exports = {
asyncWrap: {
Providers: async_wrap.Providers,
},
kNoPromiseHook,
};
19 changes: 3 additions & 16 deletions lib/internal/inspector_async_hook.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
'use strict';

const {
SafeSet,
} = primordials;

let hook;
let config;

function lazyHookCreation() {
const inspector = internalBinding('inspector');
const { createHook } = require('async_hooks');
config = internalBinding('config');
const { kNoPromiseHook } = require('internal/async_hooks');

hook = createHook({
init(asyncId, type, triggerAsyncId, resource) {
Expand All @@ -19,32 +16,22 @@ function lazyHookCreation() {
// in https://github.com/nodejs/node/pull/13870#discussion_r124515293,
// this should be fine as long as we call asyncTaskCanceled() too.
const recurring = true;
if (type === 'PROMISE')
this.promiseIds.add(asyncId);
else
inspector.asyncTaskScheduled(type, asyncId, recurring);
inspector.asyncTaskScheduled(type, asyncId, recurring);
},

before(asyncId) {
if (this.promiseIds.has(asyncId))
return;
inspector.asyncTaskStarted(asyncId);
},

after(asyncId) {
if (this.promiseIds.has(asyncId))
return;
inspector.asyncTaskFinished(asyncId);
},

destroy(asyncId) {
if (this.promiseIds.has(asyncId))
return this.promiseIds.delete(asyncId);
inspector.asyncTaskCanceled(asyncId);
},
});

hook.promiseIds = new SafeSet();
hook[kNoPromiseHook] = true;
}

function enable() {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-inspector-async-call-stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function verifyAsyncHookEnabled(message) {
assert.strictEqual(async_hook_fields[kTotals], 4,
`${async_hook_fields[kTotals]} !== 4: ${message}`);
const promiseHooks = getPromiseHooks();
assert.notDeepStrictEqual(
assert.deepStrictEqual( // Inspector async hooks should not enable promise hooks
promiseHooks, emptyPromiseHooks,
`${message}: promise hooks ${inspect(promiseHooks)}`
);
Expand Down
Loading