Skip to content

Commit

Permalink
worker: fix exit code for error thrown in handler
Browse files Browse the repository at this point in the history
Change worker exit code when the unhandled exception
handler throws from 0 to 7

fixes: nodejs#37996
  • Loading branch information
Linkgoron committed Mar 31, 2021
1 parent 31fe3b2 commit 7cf8425
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/internal/main/worker_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,29 @@ port.on('message', (message) => {
function workerOnGlobalUncaughtException(error, fromPromise) {
debug(`[${threadId}] gets uncaught exception`);
let handled = false;
let handlerThrew = false;
try {
handled = onGlobalUncaughtException(error, fromPromise);
} catch (e) {
error = e;
handlerThrew = true;
}
debug(`[${threadId}] uncaught exception handled = ${handled}`);

if (handled) {
return true;
}

if (!process._exiting) {
try {
process._exiting = true;
process.exitCode = handlerThrew ? 7 : 1;
if (!handlerThrew) {
process.emit('exit', process.exitCode);
}
} catch {}
}

let serialized;
try {
const { serializeError } = require('internal/error_serdes');
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/process-exit-code-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,15 @@ cases.push({
result: 0,
error: /^Error: ok$/,
});

function exitWithThrowInUncaughtHandler() {
process.on('uncaughtException', () => {
throw new Error('ok')
});
throw new Error('bad');
}
cases.push({
func: exitWithThrowInUncaughtHandler,
result: 7,
error: /^Error: ok$/,
});

0 comments on commit 7cf8425

Please sign in to comment.