Skip to content

Commit dab3d71

Browse files
addaleaxrvagg
authored andcommitted
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: #26111 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 778db67 commit dab3d71

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

src/api/environment.cc

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ static bool AllowWasmCodeGenerationCallback(Local<Context> context,
3232
static bool ShouldAbortOnUncaughtException(Isolate* isolate) {
3333
HandleScope scope(isolate);
3434
Environment* env = Environment::GetCurrent(isolate);
35-
return env != nullptr && env->should_abort_on_uncaught_toggle()[0] &&
35+
return env != nullptr &&
36+
(env->is_main_thread() || !env->is_stopping_worker()) &&
37+
env->should_abort_on_uncaught_toggle()[0] &&
3638
!env->inside_should_not_abort_on_uncaught_scope();
3739
}
3840

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const { spawn } = require('child_process');
5+
const { Worker } = require('worker_threads');
6+
7+
// Tests that --abort-on-uncaught-exception applies to workers as well.
8+
9+
if (process.argv[2] === 'child') {
10+
new Worker('throw new Error("foo");', { eval: true });
11+
return;
12+
}
13+
14+
const child = spawn(process.execPath, [
15+
'--abort-on-uncaught-exception', __filename, 'child'
16+
]);
17+
child.on('exit', common.mustCall((code, sig) => {
18+
if (common.isWindows) {
19+
assert.strictEqual(code, 0xC0000005);
20+
} else {
21+
assert(['SIGABRT', 'SIGTRAP', 'SIGILL'].includes(sig),
22+
`Unexpected signal ${sig}`);
23+
}
24+
}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Flags: --abort-on-uncaught-exception
2+
'use strict';
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const { Worker } = require('worker_threads');
6+
7+
// Tests that --abort-on-uncaught-exception does not apply to
8+
// termination exceptions.
9+
10+
const w = new Worker('while(true);', { eval: true });
11+
w.on('online', common.mustCall(() => w.terminate()));
12+
w.on('exit', common.mustCall((code) => assert.strictEqual(code, 1)));

0 commit comments

Comments
 (0)