From 5ac145649a77ea79ae20b7b211b3d7654922e70e Mon Sep 17 00:00:00 2001 From: Appurva Murawat Date: Thu, 29 Aug 2024 23:24:34 +0530 Subject: [PATCH] Fix `uncaughtException` event listener not being removed --- CHANGELOG.yaml | 4 ++++ lib/sandbox/execute.js | 32 +++++++++++++------------------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.yaml b/CHANGELOG.yaml index 617c6a39..7ae40d07 100644 --- a/CHANGELOG.yaml +++ b/CHANGELOG.yaml @@ -1,3 +1,7 @@ +unreleased: + fixed bugs: + - Fixed `uncaughtException` event listener not being removed + 5.1.1: date: 2024-08-01 fixed bugs: diff --git a/lib/sandbox/execute.js b/lib/sandbox/execute.js index 82643e6a..ae174015 100644 --- a/lib/sandbox/execute.js +++ b/lib/sandbox/execute.js @@ -187,31 +187,27 @@ module.exports = function (bridge, glob) { timers.clearAll(); } - // remove listener of disconnection event - bridge.off(abortEventName); - bridge.off(responseEventName); - bridge.off(cookiesEventName); - bridge.off('uncaughtException', onError); - // fire extra execution error event if (err) { onError(err); } - // @note delete response from the execution object to avoid dispatching - // the large response payload back due to performance reasons. - execution.response && (delete execution.response); + function complete () { + bridge.off(abortEventName); + bridge.off(responseEventName); + bridge.off(cookiesEventName); + bridge.off('uncaughtException', onError); - if (dnd !== true) { - const dispatchExecutionResult = () => { - bridge.dispatch(executionEventName, err || null, execution); - }; + // @note delete response from the execution object to avoid dispatching + // the large response payload back due to performance reasons. + execution.response && (delete execution.response); + bridge.dispatch(executionEventName, err || null, execution); + } + if (dnd !== true) { // if execution is skipped, we dispatch execution completion // event immediately to avoid any further processing else we // dispatch it in next tick to allow any other pending events // to be dispatched. - skippedExecution ? - dispatchExecutionResult() : - timers.wrapped.setImmediate(dispatchExecutionResult); + skippedExecution ? complete() : timers.wrapped.setImmediate(complete); } }); @@ -246,9 +242,7 @@ module.exports = function (bridge, glob) { // and one of them throws an error, this handler will be triggered // for all of them. This is a limitation of uvm as there is no way // to isolate the uncaught exception handling for each execution. - bridge.on('uncaughtException', (err) => { - onError(err); - }); + bridge.on('uncaughtException', onError); if (!options.resolvedPackages) { disabledAPIs.push('require');