Skip to content

Commit ef033d0

Browse files
BridgeARtargos
authored andcommitted
worker: fix process._fatalException return type
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>
1 parent 038cbb0 commit ef033d0

File tree

2 files changed

+46
-19
lines changed

2 files changed

+46
-19
lines changed

lib/internal/main/worker_thread.js

+21-19
Original file line numberDiff line numberDiff line change
@@ -161,26 +161,28 @@ function workerOnGlobalUncaughtException(error, fromPromise) {
161161
}
162162
debug(`[${threadId}] uncaught exception handled = ${handled}`);
163163

164-
if (!handled) {
165-
let serialized;
166-
try {
167-
const { serializeError } = require('internal/error-serdes');
168-
serialized = serializeError(error);
169-
} catch {}
170-
debug(`[${threadId}] uncaught exception serialized = ${!!serialized}`);
171-
if (serialized)
172-
port.postMessage({
173-
type: ERROR_MESSAGE,
174-
error: serialized
175-
});
176-
else
177-
port.postMessage({ type: COULD_NOT_SERIALIZE_ERROR });
178-
179-
const { clearAsyncIdStack } = require('internal/async_hooks');
180-
clearAsyncIdStack();
181-
182-
process.exit();
164+
if (handled) {
165+
return true;
183166
}
167+
168+
let serialized;
169+
try {
170+
const { serializeError } = require('internal/error-serdes');
171+
serialized = serializeError(error);
172+
} catch {}
173+
debug(`[${threadId}] uncaught exception serialized = ${!!serialized}`);
174+
if (serialized)
175+
port.postMessage({
176+
type: ERROR_MESSAGE,
177+
error: serialized
178+
});
179+
else
180+
port.postMessage({ type: COULD_NOT_SERIALIZE_ERROR });
181+
182+
const { clearAsyncIdStack } = require('internal/async_hooks');
183+
clearAsyncIdStack();
184+
185+
process.exit();
184186
}
185187

186188
// Patch the global uncaught exception handler so it gets picked up by
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const { Worker } = require('worker_threads');
5+
6+
// Check that `process._fatalException()` returns a boolean when run inside a
7+
// worker.
8+
9+
// Do not use isMainThread so that this test itself can be run inside a Worker.
10+
if (!process.env.HAS_STARTED_WORKER) {
11+
process.env.HAS_STARTED_WORKER = 1;
12+
const w = new Worker(__filename);
13+
w.on('exit', common.mustCall((code) => {
14+
assert.strictEqual(code, 0);
15+
}));
16+
return;
17+
}
18+
19+
process.once('uncaughtException', () => {
20+
process.nextTick(() => {
21+
assert.strictEqual(res, true);
22+
});
23+
});
24+
25+
const res = process._fatalException(new Error());

0 commit comments

Comments
 (0)