-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
Showing
3 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |