From 618fcbd125c386b7bfb37cbc4dce12a694a4ee22 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 7 May 2019 00:33:59 +0200 Subject: async_hooks: only disable promise hook if wanted The promise hook has been disabled asynchronously in order to solve issues when an async hook is disabled during a microtask. This means that after scheduling the disable-promise-hook call, attempts to enable it synchronously will later be unintentionally overridden. In order to solve this, make sure that the promise hooks are still no longer desired at the time at which we would disable them. Fixes: https://github.com/nodejs/node/issues/27585 PR-URL: https://github.com/nodejs/node/pull/27590 Reviewed-By: Rich Trott Reviewed-By: Ruben Bridgewater --- lib/internal/async_hooks.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'lib/internal/async_hooks.js') diff --git a/lib/internal/async_hooks.js b/lib/internal/async_hooks.js index 64f5cb2462..bf803885ca 100644 --- a/lib/internal/async_hooks.js +++ b/lib/internal/async_hooks.js @@ -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 @@ -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 // -- cgit v1.2.3