@@ -148,7 +148,7 @@ void AsyncWrap::EmitTraceEventBefore() {
148148void AsyncWrap::EmitBefore (Environment* env, double async_id,
149149 v8::Local<v8::Object> resource) {
150150 v8::Local<v8::Context> context = env->isolate ()->GetCurrentContext ();
151- env->async_hooks ()->set_execution_async_resource (resource);
151+ env->async_hooks ()->push_execution_async_resource (resource);
152152
153153 Emit (env, async_id, AsyncHooks::kBefore ,
154154 env->async_hooks_before_function ());
@@ -180,7 +180,7 @@ void AsyncWrap::EmitAfter(Environment* env, double async_id) {
180180 Emit (env, async_id, AsyncHooks::kAfter ,
181181 env->async_hooks_after_function ());
182182
183- env->async_hooks ()->clear_execution_async_resource ();
183+ env->async_hooks ()->pop_execution_async_resource ();
184184}
185185
186186class PromiseWrap : public AsyncWrap {
@@ -266,10 +266,14 @@ static void PromiseHook(PromiseHookType type, Local<Promise> promise,
266266
267267 // needed for async functions :/
268268 // the top level will not emit before and after
269- env->async_hooks ()->set_execution_async_resource (wrap->object ());
269+ env->async_hooks ()->push_execution_async_resource (wrap->object ());
270270 }
271271 }
272272
273+ if (type == PromiseHookType::kResolve && !parent->IsPromise ()) {
274+ env->async_hooks ()->pop_execution_async_resource ();
275+ }
276+
273277 if (wrap == nullptr ) return ;
274278
275279 if (type == PromiseHookType::kBefore ) {
@@ -402,11 +406,19 @@ static void GetExecutionAsyncResource(const FunctionCallbackInfo<Value>& args) {
402406 args.GetReturnValue ().Set (env->async_hooks ()->get_execution_async_resource ());
403407}
404408
405- static void SetExecutionAsyncResource (const FunctionCallbackInfo<Value>& args) {
409+ static void PushExecutionAsyncResource (
410+ const FunctionCallbackInfo<Value>& args) {
411+ Isolate* isolate = args.GetIsolate ();
412+ Environment* env = Environment::GetCurrent (args);
413+ v8::Local<v8::Context> context = isolate->GetCurrentContext ();
414+ env->async_hooks ()->push_execution_async_resource (args[0 ]);
415+ }
416+
417+ static void PopExecutionAsyncResource (const FunctionCallbackInfo<Value>& args) {
406418 Isolate* isolate = args.GetIsolate ();
407419 Environment* env = Environment::GetCurrent (args);
408420 v8::Local<v8::Context> context = isolate->GetCurrentContext ();
409- env->async_hooks ()->set_execution_async_resource (args[ 0 ] );
421+ env->async_hooks ()->pop_execution_async_resource ( );
410422}
411423
412424void AsyncWrap::GetAsyncId (const FunctionCallbackInfo<Value>& args) {
@@ -497,10 +509,12 @@ void AsyncWrap::Initialize(Local<Object> target,
497509 env->SetMethod (target, " disablePromiseHook" , DisablePromiseHook);
498510 env->SetMethod (target, " registerDestroyHook" , RegisterDestroyHook);
499511 env->SetMethod (target, " executionAsyncResource" , GetExecutionAsyncResource);
500- env->SetMethod (target, " setExecutionAsyncResource" ,
501- SetExecutionAsyncResource);
512+ env->SetMethod (target, " pushExecutionAsyncResource" ,
513+ PushExecutionAsyncResource);
514+ env->SetMethod (target, " popExecutionAsyncResource" ,
515+ PopExecutionAsyncResource);
502516
503- env->async_hooks ()->clear_execution_async_resource ( );
517+ env->async_hooks ()->push_execution_async_resource ( v8::Object::New (isolate) );
504518
505519 PropertyAttribute ReadOnlyDontDelete =
506520 static_cast <PropertyAttribute>(ReadOnly | DontDelete);
0 commit comments