Skip to content

Commit 4acd7f4

Browse files
addaleaxtargos
authored andcommitted
worker: do not emit 'exit' events during process.exit()
Do not emit `'exit'` events caused by recursively stopping all running Workers from inside the `process.exit()` call. PR-URL: #32546 Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 4e5271a commit 4acd7f4

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

src/env.cc

+1
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,7 @@ void Environment::Exit(int exit_code) {
968968
isolate(), stack_trace_limit(), StackTrace::kDetailed));
969969
}
970970
if (is_main_thread()) {
971+
set_can_call_into_js(false);
971972
stop_sub_worker_contexts();
972973
DisposePlatform();
973974
exit(exit_code);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const { Worker, workerData } = require('worker_threads');
5+
6+
// Test that 'exit' events for nested Workers are not received when a Worker
7+
// terminates itself through process.exit().
8+
9+
if (workerData === null) {
10+
const nestedWorkerExitCounter = new Int32Array(new SharedArrayBuffer(4));
11+
const w = new Worker(__filename, { workerData: nestedWorkerExitCounter });
12+
w.on('exit', common.mustCall(() => {
13+
assert.strictEqual(nestedWorkerExitCounter[0], 0);
14+
}));
15+
} else {
16+
const nestedWorker = new Worker('setInterval(() => {}, 100)', { eval: true });
17+
// The counter should never be increased here.
18+
nestedWorker.on('exit', () => workerData[0]++);
19+
nestedWorker.on('online', () => process.exit());
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const { spawnSync } = require('child_process');
5+
const { Worker } = require('worker_threads');
6+
7+
// Test that 'exit' events for Workers are not received when the main thread
8+
// terminates itself through process.exit().
9+
10+
if (process.argv[2] !== 'child') {
11+
const {
12+
stdout, stderr, status
13+
} = spawnSync(process.execPath, [__filename, 'child'], { encoding: 'utf8' });
14+
assert.strictEqual(stderr, '');
15+
assert.strictEqual(stdout, '');
16+
assert.strictEqual(status, 0);
17+
} else {
18+
const nestedWorker = new Worker('setInterval(() => {}, 100)', { eval: true });
19+
// This console.log() should never fire.
20+
nestedWorker.on('exit', () => console.log('exit event received'));
21+
nestedWorker.on('online', () => process.exit());
22+
}

0 commit comments

Comments
 (0)