mirror of
https://github.com/nodejs/node.git
synced 2026-11-30 11:55:44 -05:00
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: https://github.com/nodejs/node/pull/32546 Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
1 parent
848df6f6cc
commit
aeea7d9c1f
3 files changed
+43
No files matched your search
@@ -968,6 +968,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);
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
Reference in new issue
Block a user