Skip to content

Commit b0943a6

Browse files
lundibunditargos
authored andcommitted
worker: exit after uncaught exception
Previously even after uncaught exception the worker would continue to execute until there is no more work to do. PR-URL: #21739 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 600349a commit b0943a6

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

lib/internal/worker.js

+2
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,8 @@ function setupChild(evalScript) {
467467
else
468468
port.postMessage({ type: messageTypes.COULD_NOT_SERIALIZE_ERROR });
469469
clearAsyncIdStack();
470+
471+
process.exit();
470472
}
471473
}
472474
}

test/parallel/test-worker-uncaught-exception-async.js

+14
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,27 @@ if (!process.env.HAS_STARTED_WORKER) {
1010
const w = new Worker(__filename);
1111
w.on('message', common.mustNotCall());
1212
w.on('error', common.mustCall((err) => {
13+
console.log(err.message);
1314
assert(/^Error: foo$/.test(err));
1415
}));
1516
w.on('exit', common.mustCall((code) => {
1617
// uncaughtException is code 1
1718
assert.strictEqual(code, 1);
1819
}));
1920
} else {
21+
// cannot use common.mustCall as it cannot catch this
22+
let called = false;
23+
process.on('exit', (code) => {
24+
if (!called) {
25+
called = true;
26+
} else {
27+
assert.fail('Exit callback called twice in worker');
28+
}
29+
});
30+
31+
setTimeout(() => assert.fail('Timeout executed after uncaughtException'),
32+
2000);
33+
2034
setImmediate(() => {
2135
throw new Error('foo');
2236
});

test/parallel/test-worker-uncaught-exception.js

+4
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,9 @@ if (!process.env.HAS_STARTED_WORKER) {
2727
assert.fail('Exit callback called twice in worker');
2828
}
2929
});
30+
31+
setTimeout(() => assert.fail('Timeout executed after uncaughtException'),
32+
2000);
33+
3034
throw new Error('foo');
3135
}

0 commit comments

Comments
 (0)