worker: fix process._fatalException return type

This makes sure `process._fatalException()` returns a boolean when
run inside of a worker.

PR-URL: https://github.com/nodejs/node/pull/29706
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
This commit is contained in:
Ruben Bridgewater 2019-09-25 14:51:18 +02:00 committed by Rich Trott
parent c2ce8d0547
commit aea9a0f77d
2 changed files with 46 additions and 19 deletions

View File

@ -166,26 +166,28 @@ function workerOnGlobalUncaughtException(error, fromPromise) {
} }
debug(`[${threadId}] uncaught exception handled = ${handled}`); debug(`[${threadId}] uncaught exception handled = ${handled}`);
if (!handled) { if (handled) {
let serialized; return true;
try {
const { serializeError } = require('internal/error-serdes');
serialized = serializeError(error);
} catch {}
debug(`[${threadId}] uncaught exception serialized = ${!!serialized}`);
if (serialized)
port.postMessage({
type: ERROR_MESSAGE,
error: serialized
});
else
port.postMessage({ type: COULD_NOT_SERIALIZE_ERROR });
const { clearAsyncIdStack } = require('internal/async_hooks');
clearAsyncIdStack();
process.exit();
} }
let serialized;
try {
const { serializeError } = require('internal/error-serdes');
serialized = serializeError(error);
} catch {}
debug(`[${threadId}] uncaught exception serialized = ${!!serialized}`);
if (serialized)
port.postMessage({
type: ERROR_MESSAGE,
error: serialized
});
else
port.postMessage({ type: COULD_NOT_SERIALIZE_ERROR });
const { clearAsyncIdStack } = require('internal/async_hooks');
clearAsyncIdStack();
process.exit();
} }
// Patch the global uncaught exception handler so it gets picked up by // Patch the global uncaught exception handler so it gets picked up by

View File

@ -0,0 +1,25 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const { Worker } = require('worker_threads');
// Check that `process._fatalException()` returns a boolean when run inside a
// worker.
// Do not use isMainThread so that this test itself can be run inside a Worker.
if (!process.env.HAS_STARTED_WORKER) {
process.env.HAS_STARTED_WORKER = 1;
const w = new Worker(__filename);
w.on('exit', common.mustCall((code) => {
assert.strictEqual(code, 0);
}));
return;
}
process.once('uncaughtException', () => {
process.nextTick(() => {
assert.strictEqual(res, true);
});
});
const res = process._fatalException(new Error());