Skip to content

Commit

Permalink
worker: fix process._fatalException return type
Browse files Browse the repository at this point in the history
This makes sure `process._fatalException()` returns a boolean when
run inside of a worker.

PR-URL: #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>
  • Loading branch information
BridgeAR authored and targos committed Oct 1, 2019
1 parent 038cbb0 commit ef033d0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
40 changes: 21 additions & 19 deletions lib/internal/main/worker_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,26 +161,28 @@ function workerOnGlobalUncaughtException(error, fromPromise) {
}
debug(`[${threadId}] uncaught exception handled = ${handled}`);

if (!handled) {
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();
if (handled) {
return true;
}

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
Expand Down
25 changes: 25 additions & 0 deletions test/parallel/test-worker-non-fatal-uncaught-exception.js
Original file line number Diff line number Diff line change
@@ -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());

0 comments on commit ef033d0

Please sign in to comment.