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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

async_hooks: switch between native and context hooks correctly #38912

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions lib/internal/async_hooks.js
Expand Up @@ -357,7 +357,9 @@ function updatePromiseHookMode() {
wantPromiseHook = true;
if (destroyHooksExist()) {
enablePromiseHook();
setPromiseHooks(undefined, undefined, undefined, undefined);
} else {
disablePromiseHook();
setPromiseHooks(
initHooksExist() ? promiseInitHook : undefined,
promiseBeforeHook,
Expand Down
77 changes: 77 additions & 0 deletions test/parallel/test-async-hooks-correctly-switch-promise-hook.js
@@ -0,0 +1,77 @@
'use strict';
require('../common');
const assert = require('assert');
const async_hooks = require('async_hooks');

// Regression test for:
// - https://github.com/nodejs/node/issues/38814
// - https://github.com/nodejs/node/issues/38815

const layers = new Map();

// Only init to start context-based promise hook
async_hooks.createHook({
init(asyncId, type) {
layers.set(asyncId, {
type,
init: true,
before: false,
after: false,
promiseResolve: false
});
},
before(asyncId) {
if (layers.has(asyncId)) {
layers.get(asyncId).before = true;
}
},
after(asyncId) {
if (layers.has(asyncId)) {
layers.get(asyncId).after = true;
}
},
promiseResolve(asyncId) {
if (layers.has(asyncId)) {
layers.get(asyncId).promiseResolve = true;
}
}
}).enable();

// With destroy, this should switch to native
// and disable context - based promise hook
async_hooks.createHook({
init() { },
destroy() { }
}).enable();

async function main() {
return Promise.resolve();
}

main();

process.on('exit', () => {
assert.deepStrictEqual(Array.from(layers.values()), [
{
type: 'PROMISE',
init: true,
before: true,
after: true,
promiseResolve: true
},
{
type: 'PROMISE',
init: true,
before: false,
after: false,
promiseResolve: true
},
{
type: 'PROMISE',
init: true,
before: true,
after: true,
promiseResolve: true
},
]);
});