diff --git a/src/api/environment.cc b/src/api/environment.cc index f93fcd738d3179..37028dff6260fc 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)));