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

cluster: fix cluster rr distribute error when accept fails. #44202

Merged
merged 1 commit into from
Aug 28, 2022
Merged
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
4 changes: 4 additions & 0 deletions lib/internal/cluster/round_robin_handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ RoundRobinHandle.prototype.remove = function(worker) {
};

RoundRobinHandle.prototype.distribute = function(err, handle) {
// If `accept` fails just skip it (handle is undefined)
if (err) {
return;
}
append(this.handles, handle);
// eslint-disable-next-line node-core/no-array-destructuring
const [ workerEntry ] = this.free; // this.free is a SafeMap
Expand Down
30 changes: 30 additions & 0 deletions test/parallel/test-cluster-accept-fail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Flags: --expose-internals
'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');
const cluster = require('cluster');
const rr = require('internal/cluster/round_robin_handle');

if (cluster.isPrimary) {
const distribute = rr.prototype.distribute;
rr.prototype.distribute = function(err, handle) {
assert.strictEqual(err, 0);
handle.close();
distribute.call(this, -1, undefined);
};
cluster.schedulingPolicy = cluster.SCHED_RR;
cluster.fork();
} else {
const server = net.createServer(common.mustNotCall());
server.listen(0, common.mustCall(() => {

const socket = net.connect(server.address().port);

socket.on('close', common.mustCall(() => {
server.close(common.mustCall(() => {
process.disconnect();
}));
}));
}));
}