Skip to content

Commit aea9a0f

Browse files
BridgeARTrott
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 <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Minwoo Jung <[email protected]>
1 parent c2ce8d0 commit aea9a0f

File tree

2 files changed

+46
-19
lines changed

2 files changed

+46
-19
lines changed

lib/internal/main/worker_thread.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -166,26 +166,28 @@ function workerOnGlobalUncaughtException(error, fromPromise) {
166166
}
167167
debug(`[${threadId}] uncaught exception handled = ${handled}`);
168168

169-
if (!handled) {
170-
let serialized;
171-
try {
172-
const { serializeError } = require('internal/error-serdes');
173-
serialized = serializeError(error);
174-
} catch {}
175-
debug(`[${threadId}] uncaught exception serialized = ${!!serialized}`);
176-
if (serialized)
177-
port.postMessage({
178-
type: ERROR_MESSAGE,
179-
error: serialized
180-
});
181-
else
182-
port.postMessage({ type: COULD_NOT_SERIALIZE_ERROR });
183-
184-
const { clearAsyncIdStack } = require('internal/async_hooks');
185-
clearAsyncIdStack();
186-
187-
process.exit();
169+
if (handled) {
170+
return true;
188171
}
172+
173+
let serialized;
174+
try {
175+
const { serializeError } = require('internal/error-serdes');
176+
serialized = serializeError(error);
177+
} catch {}
178+
debug(`[${threadId}] uncaught exception serialized = ${!!serialized}`);
179+
if (serialized)
180+
port.postMessage({
181+
type: ERROR_MESSAGE,
182+
error: serialized
183+
});
184+
else
185+
port.postMessage({ type: COULD_NOT_SERIALIZE_ERROR });
186+
187+
const { clearAsyncIdStack } = require('internal/async_hooks');
188+
clearAsyncIdStack();
189+
190+
process.exit();
189191
}
190192

191193
// Patch the global uncaught exception handler so it gets picked up by
Lines changed: 25 additions & 0 deletions
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)