From dab3d71243136cd2e56873001d5e1a05a98e6fe7 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 14 Feb 2019 23:30:37 +0100 Subject: [PATCH] worker: ignore --abort-on-uncaught-exception for terminate() When running Worker threads with `--abort-on-uncaught-exception`, do not abort the process when `worker.terminate()` is called. PR-URL: https://github.com/nodejs/node/pull/26111 Reviewed-By: James M Snell Reviewed-By: Joyee Cheung --- src/api/environment.cc | 4 +++- .../test-worker-abort-uncaught-exception.js | 24 +++++++++++++++++++ ...r-abort-on-uncaught-exception-terminate.js | 12 ++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 test/abort/test-worker-abort-uncaught-exception.js create mode 100644 test/parallel/test-worker-abort-on-uncaught-exception-terminate.js diff --git a/src/api/environment.cc b/src/api/environment.cc index a1320c0761d0b5..ee9e623faa6cb6 100644 --- a/src/api/environment.cc +++ b/src/api/environment.cc @@ -32,7 +32,9 @@ static bool AllowWasmCodeGenerationCallback(Local context, static bool ShouldAbortOnUncaughtException(Isolate* isolate) { HandleScope scope(isolate); Environment* env = Environment::GetCurrent(isolate); - return env != nullptr && env->should_abort_on_uncaught_toggle()[0] && + return env != nullptr && + (env->is_main_thread() || !env->is_stopping_worker()) && + env->should_abort_on_uncaught_toggle()[0] && !env->inside_should_not_abort_on_uncaught_scope(); } diff --git a/test/abort/test-worker-abort-uncaught-exception.js b/test/abort/test-worker-abort-uncaught-exception.js new file mode 100644 index 00000000000000..63f7acdddc7fd7 --- /dev/null +++ b/test/abort/test-worker-abort-uncaught-exception.js @@ -0,0 +1,24 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const { spawn } = require('child_process'); +const { Worker } = require('worker_threads'); + +// Tests that --abort-on-uncaught-exception applies to workers as well. + +if (process.argv[2] === 'child') { + new Worker('throw new Error("foo");', { eval: true }); + return; +} + +const child = spawn(process.execPath, [ + '--abort-on-uncaught-exception', __filename, 'child' +]); +child.on('exit', common.mustCall((code, sig) => { + if (common.isWindows) { + assert.strictEqual(code, 0xC0000005); + } else { + assert(['SIGABRT', 'SIGTRAP', 'SIGILL'].includes(sig), + `Unexpected signal ${sig}`); + } +})); diff --git a/test/parallel/test-worker-abort-on-uncaught-exception-terminate.js b/test/parallel/test-worker-abort-on-uncaught-exception-terminate.js new file mode 100644 index 00000000000000..de73b5e4e34e47 --- /dev/null +++ b/test/parallel/test-worker-abort-on-uncaught-exception-terminate.js @@ -0,0 +1,12 @@ +// Flags: --abort-on-uncaught-exception +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const { Worker } = require('worker_threads'); + +// Tests that --abort-on-uncaught-exception does not apply to +// termination exceptions. + +const w = new Worker('while(true);', { eval: true }); +w.on('online', common.mustCall(() => w.terminate())); +w.on('exit', common.mustCall((code) => assert.strictEqual(code, 1)));