Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

worker: do not emit 'exit' events during process.exit() #32546

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,7 @@ void Environment::Exit(int exit_code) {
isolate(), stack_trace_limit(), StackTrace::kDetailed));
}
if (is_main_thread()) {
set_can_call_into_js(false);
stop_sub_worker_contexts();
DisposePlatform();
exit(exit_code);
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-worker-nested-on-process-exit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const { Worker, workerData } = require('worker_threads');

// Test that 'exit' events for nested Workers are not received when a Worker
// terminates itself through process.exit().

if (workerData === null) {
const nestedWorkerExitCounter = new Int32Array(new SharedArrayBuffer(4));
const w = new Worker(__filename, { workerData: nestedWorkerExitCounter });
w.on('exit', common.mustCall(() => {
assert.strictEqual(nestedWorkerExitCounter[0], 0);
}));
} else {
const nestedWorker = new Worker('setInterval(() => {}, 100)', { eval: true });
// The counter should never be increased here.
nestedWorker.on('exit', () => workerData[0]++);
nestedWorker.on('online', () => process.exit());
}
22 changes: 22 additions & 0 deletions test/parallel/test-worker-on-process-exit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';
require('../common');
const assert = require('assert');
const { spawnSync } = require('child_process');
const { Worker } = require('worker_threads');

// Test that 'exit' events for Workers are not received when the main thread
// terminates itself through process.exit().

if (process.argv[2] !== 'child') {
const {
stdout, stderr, status
} = spawnSync(process.execPath, [__filename, 'child'], { encoding: 'utf8' });
assert.strictEqual(stderr, '');
assert.strictEqual(stdout, '');
assert.strictEqual(status, 0);
} else {
const nestedWorker = new Worker('setInterval(() => {}, 100)', { eval: true });
// This console.log() should never fire.
nestedWorker.on('exit', () => console.log('exit event received'));
nestedWorker.on('online', () => process.exit());
}