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: only disable promise hook if wanted #27590

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions lib/internal/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const active_hooks = {
};

const { registerDestroyHook } = async_wrap;
const { enqueueMicrotask } = internalBinding('task_queue');

// Each constant tracks how many callbacks there are for any given step of
// async execution. These are tracked so if the user didn't include callbacks
Expand Down Expand Up @@ -231,14 +232,26 @@ function restoreActiveHooks() {
}


let wantPromiseHook = false;
function enableHooks() {
enablePromiseHook();
async_hook_fields[kCheck] += 1;

wantPromiseHook = true;
enablePromiseHook();
}

function disableHooks() {
disablePromiseHook();
async_hook_fields[kCheck] -= 1;

wantPromiseHook = false;
// Delay the call to `disablePromiseHook()` because we might currently be
// between the `before` and `after` calls of a Promise.
enqueueMicrotask(disablePromiseHookIfNecessary);
}

function disablePromiseHookIfNecessary() {
if (!wantPromiseHook)
disablePromiseHook();
}

// Internal Embedder API //
Expand Down
13 changes: 4 additions & 9 deletions src/async_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -331,15 +331,10 @@ static void EnablePromiseHook(const FunctionCallbackInfo<Value>& args) {
static void DisablePromiseHook(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();

// Delay the call to `RemovePromiseHook` because we might currently be
// between the `before` and `after` calls of a Promise.
isolate->EnqueueMicrotask([](void* data) {
// The per-Isolate API provides no way of knowing whether there are multiple
// users of the PromiseHook. That hopefully goes away when V8 introduces
// a per-context API.
Isolate* isolate = static_cast<Isolate*>(data);
isolate->SetPromiseHook(nullptr);
}, static_cast<void*>(isolate));
// The per-Isolate API provides no way of knowing whether there are multiple
// users of the PromiseHook. That hopefully goes away when V8 introduces
// a per-context API.
isolate->SetPromiseHook(nullptr);
}


Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-async-hooks-enable-disable-enable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const async_hooks = require('async_hooks');

// Regression test for https://github.com/nodejs/node/issues/27585.

async_hooks.createHook({ init: () => {} }).enable().disable().enable();
async_hooks.createHook({ init: () => {} }).enable();

async function main() {
const initialAsyncId = async_hooks.executionAsyncId();
await 0;
assert.notStrictEqual(async_hooks.executionAsyncId(), initialAsyncId);
}

main().then(common.mustCall());