-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cluster: fix error on worker disconnect/destroy
Avoid sending multiple `exitedAfterDisconnect` messages when concurrently calling `disconnect()` and/or `destroy()` from the worker so `ERR_IPC_DISCONNECTED` errors are not generated. Fixes: #32106 PR-URL: #32793 Reviewed-By: Zeyu Yang <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
- Loading branch information
1 parent
ee9280a
commit 73324cf
Showing
2 changed files
with
57 additions
and
2 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,48 @@ | ||
'use strict'; | ||
|
||
// Ref: https://github.com/nodejs/node/issues/32106 | ||
|
||
const common = require('../common'); | ||
|
||
const assert = require('assert'); | ||
const cluster = require('cluster'); | ||
const os = require('os'); | ||
|
||
if (cluster.isMaster) { | ||
const workers = []; | ||
const numCPUs = os.cpus().length; | ||
let waitOnline = numCPUs; | ||
for (let i = 0; i < numCPUs; i++) { | ||
const worker = cluster.fork(); | ||
workers[i] = worker; | ||
worker.once('online', common.mustCall(() => { | ||
if (--waitOnline === 0) | ||
for (const worker of workers) | ||
if (worker.isConnected()) | ||
worker.send(i % 2 ? 'disconnect' : 'destroy'); | ||
})); | ||
|
||
// These errors can occur due to the nature of the test, we might be trying | ||
// to send messages when the worker is disconnecting. | ||
worker.on('error', (err) => { | ||
assert.strictEqual(err.syscall, 'write'); | ||
assert.strictEqual(err.code, 'EPIPE'); | ||
}); | ||
|
||
worker.once('disconnect', common.mustCall(() => { | ||
for (const worker of workers) | ||
if (worker.isConnected()) | ||
worker.send('disconnect'); | ||
})); | ||
|
||
worker.once('exit', common.mustCall((code, signal) => { | ||
assert.strictEqual(code, 0); | ||
assert.strictEqual(signal, null); | ||
})); | ||
} | ||
} else { | ||
process.on('message', (msg) => { | ||
if (cluster.worker.isConnected()) | ||
cluster.worker[msg](); | ||
}); | ||
} |