Skip to content

Commit

Permalink
worker: exit after uncaught exception
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
lundibundi authored and targos committed Jul 14, 2018
1 parent 600349a commit b0943a6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,8 @@ function setupChild(evalScript) {
else
port.postMessage({ type: messageTypes.COULD_NOT_SERIALIZE_ERROR });
clearAsyncIdStack();

process.exit();
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-worker-uncaught-exception-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,27 @@ if (!process.env.HAS_STARTED_WORKER) {
const w = new Worker(__filename);
w.on('message', common.mustNotCall());
w.on('error', common.mustCall((err) => {
console.log(err.message);
assert(/^Error: foo$/.test(err));
}));
w.on('exit', common.mustCall((code) => {
// uncaughtException is code 1
assert.strictEqual(code, 1);
}));
} else {
// cannot use common.mustCall as it cannot catch this
let called = false;
process.on('exit', (code) => {
if (!called) {
called = true;
} else {
assert.fail('Exit callback called twice in worker');
}
});

setTimeout(() => assert.fail('Timeout executed after uncaughtException'),
2000);

setImmediate(() => {
throw new Error('foo');
});
Expand Down
4 changes: 4 additions & 0 deletions test/parallel/test-worker-uncaught-exception.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,9 @@ if (!process.env.HAS_STARTED_WORKER) {
assert.fail('Exit callback called twice in worker');
}
});

setTimeout(() => assert.fail('Timeout executed after uncaughtException'),
2000);

throw new Error('foo');
}

0 comments on commit b0943a6

Please sign in to comment.