diff options
author | Anna Henningsen <anna@addaleax.net> | 2019-05-07 00:33:59 +0200 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2019-05-13 12:47:45 +0200 |
commit | 618fcbd125c386b7bfb37cbc4dce12a694a4ee22 (patch) | |
tree | 4dd0cf1c4bb28ace5e2214f4e5babd47d4c48aa9 /lib/internal/async_hooks.js | |
parent | 3309c856bcc88751ee3b4512f9a9b95fe903e361 (diff) | |
download | android-node-v8-618fcbd125c386b7bfb37cbc4dce12a694a4ee22.tar.gz android-node-v8-618fcbd125c386b7bfb37cbc4dce12a694a4ee22.tar.bz2 android-node-v8-618fcbd125c386b7bfb37cbc4dce12a694a4ee22.zip |
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 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 // |