diff options
author | Anna Henningsen <anna@addaleax.net> | 2019-05-07 01:33:59 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2019-05-13 13:47:45 +0300 |
commit | 618fcbd125c386b7bfb37cbc4dce12a694a4ee22 (patch) | |
tree | 4dd0cf1c4bb28ace5e2214f4e5babd47d4c48aa9 /lib/internal/async_hooks.js | |
parent | 3309c856bcc88751ee3b4512f9a9b95fe903e361 (diff) |
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 <rtrott@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Diffstat (limited to 'lib/internal/async_hooks.js')
-rw-r--r-- | lib/internal/async_hooks.js | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/lib/internal/async_hooks.js b/lib/internal/async_hooks.js index 64f5cb2462f..bf803885cac 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 // |