Skip to content

Commit e05c29d

Browse files
santigimenotargos
authored andcommitted
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]>
1 parent dad8217 commit e05c29d

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

lib/internal/cluster/child.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,16 +228,23 @@ function _disconnect(masterInitiated) {
228228

229229
// Extend generic Worker with methods specific to worker processes.
230230
Worker.prototype.disconnect = function() {
231-
_disconnect.call(this);
231+
if (![ 'disconnecting', 'destroying' ].includes(this.state)) {
232+
this.state = 'disconnecting';
233+
_disconnect.call(this);
234+
}
235+
232236
return this;
233237
};
234238

235239
Worker.prototype.destroy = function() {
236-
this.exitedAfterDisconnect = true;
240+
if (this.state === 'destroying')
241+
return;
237242

243+
this.exitedAfterDisconnect = true;
238244
if (!this.isConnected()) {
239245
process.exit(0);
240246
} else {
247+
this.state = 'destroying';
241248
send({ act: 'exitedAfterDisconnect' }, () => process.disconnect());
242249
process.once('disconnect', () => process.exit(0));
243250
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
3+
// Ref: https://github.com/nodejs/node/issues/32106
4+
5+
const common = require('../common');
6+
7+
const assert = require('assert');
8+
const cluster = require('cluster');
9+
const os = require('os');
10+
11+
if (cluster.isMaster) {
12+
const workers = [];
13+
const numCPUs = os.cpus().length;
14+
let waitOnline = numCPUs;
15+
for (let i = 0; i < numCPUs; i++) {
16+
const worker = cluster.fork();
17+
workers[i] = worker;
18+
worker.once('online', common.mustCall(() => {
19+
if (--waitOnline === 0)
20+
for (const worker of workers)
21+
if (worker.isConnected())
22+
worker.send(i % 2 ? 'disconnect' : 'destroy');
23+
}));
24+
25+
// These errors can occur due to the nature of the test, we might be trying
26+
// to send messages when the worker is disconnecting.
27+
worker.on('error', (err) => {
28+
assert.strictEqual(err.syscall, 'write');
29+
assert.strictEqual(err.code, 'EPIPE');
30+
});
31+
32+
worker.once('disconnect', common.mustCall(() => {
33+
for (const worker of workers)
34+
if (worker.isConnected())
35+
worker.send('disconnect');
36+
}));
37+
38+
worker.once('exit', common.mustCall((code, signal) => {
39+
assert.strictEqual(code, 0);
40+
assert.strictEqual(signal, null);
41+
}));
42+
}
43+
} else {
44+
process.on('message', (msg) => {
45+
if (cluster.worker.isConnected())
46+
cluster.worker[msg]();
47+
});
48+
}

0 commit comments

Comments
 (0)