File tree Expand file tree Collapse file tree 3 files changed +36
-11
lines changed
Expand file tree Collapse file tree 3 files changed +36
-11
lines changed Original file line number Diff line number Diff line change @@ -69,6 +69,7 @@ const active_hooks = {
6969} ;
7070
7171const { registerDestroyHook } = async_wrap ;
72+ const { enqueueMicrotask } = internalBinding ( 'task_queue' ) ;
7273
7374// Each constant tracks how many callbacks there are for any given step of
7475// async execution. These are tracked so if the user didn't include callbacks
@@ -231,14 +232,26 @@ function restoreActiveHooks() {
231232}
232233
233234
235+ let wantPromiseHook = false ;
234236function enableHooks ( ) {
235- enablePromiseHook ( ) ;
236237 async_hook_fields [ kCheck ] += 1 ;
238+
239+ wantPromiseHook = true ;
240+ enablePromiseHook ( ) ;
237241}
238242
239243function disableHooks ( ) {
240- disablePromiseHook ( ) ;
241244 async_hook_fields [ kCheck ] -= 1 ;
245+
246+ wantPromiseHook = false ;
247+ // Delay the call to `disablePromiseHook()` because we might currently be
248+ // between the `before` and `after` calls of a Promise.
249+ enqueueMicrotask ( disablePromiseHookIfNecessary ) ;
250+ }
251+
252+ function disablePromiseHookIfNecessary ( ) {
253+ if ( ! wantPromiseHook )
254+ disablePromiseHook ( ) ;
242255}
243256
244257// Internal Embedder API //
Original file line number Diff line number Diff line change @@ -331,15 +331,10 @@ static void EnablePromiseHook(const FunctionCallbackInfo<Value>& args) {
331331static void DisablePromiseHook (const FunctionCallbackInfo<Value>& args) {
332332 Isolate* isolate = args.GetIsolate ();
333333
334- // Delay the call to `RemovePromiseHook` because we might currently be
335- // between the `before` and `after` calls of a Promise.
336- isolate->EnqueueMicrotask ([](void * data) {
337- // The per-Isolate API provides no way of knowing whether there are multiple
338- // users of the PromiseHook. That hopefully goes away when V8 introduces
339- // a per-context API.
340- Isolate* isolate = static_cast <Isolate*>(data);
341- isolate->SetPromiseHook (nullptr );
342- }, static_cast <void *>(isolate));
334+ // The per-Isolate API provides no way of knowing whether there are multiple
335+ // users of the PromiseHook. That hopefully goes away when V8 introduces
336+ // a per-context API.
337+ isolate->SetPromiseHook (nullptr );
343338}
344339
345340
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+ const common = require ( '../common' ) ;
3+ const assert = require ( 'assert' ) ;
4+ const async_hooks = require ( 'async_hooks' ) ;
5+
6+ // Regression test for https://github.com/nodejs/node/issues/27585.
7+
8+ async_hooks . createHook ( { init : ( ) => { } } ) . enable ( ) . disable ( ) . enable ( ) ;
9+ async_hooks . createHook ( { init : ( ) => { } } ) . enable ( ) ;
10+
11+ async function main ( ) {
12+ const initialAsyncId = async_hooks . executionAsyncId ( ) ;
13+ await 0 ;
14+ assert . notStrictEqual ( async_hooks . executionAsyncId ( ) , initialAsyncId ) ;
15+ }
16+
17+ main ( ) . then ( common . mustCall ( ) ) ;
You can’t perform that action at this time.
0 commit comments