From 0b14fd6d8b74a1e58705122de1015f45da28905a Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Sat, 14 Dec 2019 13:53:03 -0500 Subject: [PATCH] async_hooks: proper handling for AH in runInAsyncScope We should never try to manually run emitAfter in case of an error, the exception handler will do it for us, if we're going to recover. --- lib/async_hooks.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/async_hooks.js b/lib/async_hooks.js index be32f6d1102bbd..31e4ad4e3e56d0 100644 --- a/lib/async_hooks.js +++ b/lib/async_hooks.js @@ -21,7 +21,6 @@ const { executionAsyncId, triggerAsyncId, // Private API - hasAsyncIdStack, getHookArrays, enableHooks, disableHooks, @@ -172,14 +171,13 @@ class AsyncResource { runInAsyncScope(fn, thisArg, ...args) { const asyncId = this[async_id_symbol]; emitBefore(asyncId, this[trigger_async_id_symbol]); - try { - if (thisArg === undefined) - return fn(...args); - return ReflectApply(fn, thisArg, args); - } finally { - if (hasAsyncIdStack()) - emitAfter(asyncId); - } + + const ret = thisArg === undefined ? + fn(...args) : + ReflectApply(fn, thisArg, args); + + emitAfter(asyncId); + return ret; } emitDestroy() {