From e96fa95b9499499a2bd63c24abf782c2efa628c0 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Tue, 1 Dec 2020 21:34:08 -0800 Subject: [PATCH] fix: properly emit after hooks after exception (#26752) --- shell/common/node_bindings.cc | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/shell/common/node_bindings.cc b/shell/common/node_bindings.cc index 0026f826e625..b61cb69dc2e8 100644 --- a/shell/common/node_bindings.cc +++ b/shell/common/node_bindings.cc @@ -176,14 +176,18 @@ void ErrorMessageListener(v8::Local message, v8::Isolate* isolate = v8::Isolate::GetCurrent(); node::Environment* env = node::Environment::GetCurrent(isolate); - // TODO(codebytere): properly emit the after() hooks now - // that the exception has been handled. - // See node/lib/internal/process/execution.js#L176-L180 - - // Ensure that the async id stack is properly cleared so the async - // hook stack does not become corrupted. - if (env) { + // Emit the after() hooks now that the exception has been handled. + // Analogous to node/lib/internal/process/execution.js#L176-L180 + if (env->async_hooks()->fields()[node::AsyncHooks::kAfter]) { + while (env->async_hooks()->fields()[node::AsyncHooks::kStackLength]) { + node::AsyncWrap::EmitAfter(env, env->execution_async_id()); + env->async_hooks()->pop_async_context(env->execution_async_id()); + } + } + + // Ensure that the async id stack is properly cleared so the async + // hook stack does not become corrupted. env->async_hooks()->clear_async_id_stack(); } }