From b0943a655efee2b1ca023cac9668a788072d07c2 Mon Sep 17 00:00:00 2001 From: Denys Otrishko Date: Fri, 13 Jul 2018 18:45:18 +0300 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/21739 Reviewed-By: Anna Henningsen Reviewed-By: Ruben Bridgewater --- lib/internal/worker.js | 2 ++ .../test-worker-uncaught-exception-async.js | 14 ++++++++++++++ test/parallel/test-worker-uncaught-exception.js | 4 ++++ 3 files changed, 20 insertions(+) diff --git a/lib/internal/worker.js b/lib/internal/worker.js index bcc864b5b8b330..26c16f86e3e8fc 100644 --- a/lib/internal/worker.js +++ b/lib/internal/worker.js @@ -467,6 +467,8 @@ function setupChild(evalScript) { else port.postMessage({ type: messageTypes.COULD_NOT_SERIALIZE_ERROR }); clearAsyncIdStack(); + + process.exit(); } } } diff --git a/test/parallel/test-worker-uncaught-exception-async.js b/test/parallel/test-worker-uncaught-exception-async.js index f820976c11ebcd..862b1a66d619c3 100644 --- a/test/parallel/test-worker-uncaught-exception-async.js +++ b/test/parallel/test-worker-uncaught-exception-async.js @@ -10,6 +10,7 @@ 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) => { @@ -17,6 +18,19 @@ if (!process.env.HAS_STARTED_WORKER) { 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'); }); diff --git a/test/parallel/test-worker-uncaught-exception.js b/test/parallel/test-worker-uncaught-exception.js index 8193dccbd1ff2f..95c142b6c70d64 100644 --- a/test/parallel/test-worker-uncaught-exception.js +++ b/test/parallel/test-worker-uncaught-exception.js @@ -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'); }