From ddff2b2b22c5b7691c100ad4eea07ca926bcea05 Mon Sep 17 00:00:00 2001 From: Gireesh Punathil Date: Sun, 11 Oct 2020 08:56:06 -0400 Subject: [PATCH] lib: honor setUncaughtExceptionCaptureCallback This api does not alter the behavior of diagnostic report configured on uncaught exceptions. This is deemed as a bug. Honor this API. Refs: https://github.com/nodejs/node/pull/35588 PR-URL: https://github.com/nodejs/node/pull/35595 Reviewed-By: James M Snell Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: Joyee Cheung Reviewed-By: Richard Lau --- lib/internal/process/execution.js | 9 ++++++- ...test-report-uncaught-exception-override.js | 26 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test/report/test-report-uncaught-exception-override.js diff --git a/lib/internal/process/execution.js b/lib/internal/process/execution.js index 1087afadc5d659..314b8b0a03c161 100644 --- a/lib/internal/process/execution.js +++ b/lib/internal/process/execution.js @@ -92,12 +92,16 @@ function evalScript(name, body, breakFirstLine, print) { globalThis.module = origModule; } -const exceptionHandlerState = { captureFn: null }; +const exceptionHandlerState = { + captureFn: null, + reportFlag: false +}; function setUncaughtExceptionCaptureCallback(fn) { if (fn === null) { exceptionHandlerState.captureFn = fn; shouldAbortOnUncaughtToggle[0] = 1; + process.report.reportOnUncaughtException = exceptionHandlerState.reportFlag; return; } if (typeof fn !== 'function') { @@ -108,6 +112,9 @@ function setUncaughtExceptionCaptureCallback(fn) { } exceptionHandlerState.captureFn = fn; shouldAbortOnUncaughtToggle[0] = 0; + exceptionHandlerState.reportFlag = + process.report.reportOnUncaughtException === true; + process.report.reportOnUncaughtException = false; } function hasUncaughtExceptionCaptureCallback() { diff --git a/test/report/test-report-uncaught-exception-override.js b/test/report/test-report-uncaught-exception-override.js new file mode 100644 index 00000000000000..df4f8a1958114b --- /dev/null +++ b/test/report/test-report-uncaught-exception-override.js @@ -0,0 +1,26 @@ +// Flags: --report-uncaught-exception +'use strict'; +// Test report is suppressed on uncaught exception hook. +const common = require('../common'); +const assert = require('assert'); +const helper = require('../common/report'); +const tmpdir = require('../common/tmpdir'); +const error = new Error('test error'); + +tmpdir.refresh(); +process.report.directory = tmpdir.path; + +// First, install an uncaught exception hook. +process.setUncaughtExceptionCaptureCallback(common.mustCall()); + +// Make sure this is ignored due to the above override. +process.on('uncaughtException', common.mustNotCall()); + +process.on('exit', (code) => { + assert.strictEqual(code, 0); + // Make sure no reports are generated. + const reports = helper.findReports(process.pid, tmpdir.path); + assert.strictEqual(reports.length, 0); +}); + +throw error;