Skip to content

Commit

Permalink
worker: ignore --abort-on-uncaught-exception for terminate()
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
addaleax authored and rvagg committed Feb 28, 2019
1 parent 778db67 commit dab3d71
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/api/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ static bool AllowWasmCodeGenerationCallback(Local<Context> 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();
}

Expand Down
24 changes: 24 additions & 0 deletions test/abort/test-worker-abort-uncaught-exception.js
Original file line number Diff line number Diff line change
@@ -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}`);
}
}));
12 changes: 12 additions & 0 deletions test/parallel/test-worker-abort-on-uncaught-exception-terminate.js
Original file line number Diff line number Diff line change
@@ -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)));

0 comments on commit dab3d71

Please sign in to comment.