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

fix(cluster): respect the backlog from workers #33827

Closed
wants to merge 2 commits into from
Closed
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 doc/api/net.md
Original file line number Diff line number Diff line change
@@ -367,6 +367,10 @@ server.listen({
});
```

When `exclusive` is `true` and the underlying handle is shared, it's
possible that several workers query a handle with different backlogs.
In this case, the first `backlog` passed to the master process will be used.

Starting an IPC server as root may cause the server path to be inaccessible for
unprivileged users. Using `readableAll` and `writableAll` will make the server
accessible for all users.
7 changes: 4 additions & 3 deletions lib/internal/cluster/round_robin_handle.js
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ const { constants } = internalBinding('tcp_wrap');

module.exports = RoundRobinHandle;

function RoundRobinHandle(key, address, { port, fd, flags }) {
function RoundRobinHandle(key, address, { port, fd, flags, backlog }) {
this.key = key;
this.all = new Map();
this.free = new Map();
@@ -22,16 +22,17 @@ function RoundRobinHandle(key, address, { port, fd, flags }) {
this.server = net.createServer(assert.fail);

if (fd >= 0)
this.server.listen({ fd });
this.server.listen({ fd, backlog });
else if (port >= 0) {
this.server.listen({
port,
host: address,
// Currently, net module only supports `ipv6Only` option in `flags`.
ipv6Only: Boolean(flags & constants.UV_TCP_IPV6ONLY),
backlog,
});
} else
this.server.listen(address); // UNIX socket path.
this.server.listen(address, backlog); // UNIX socket path.

this.server.once('listening', () => {
this.handle = this.server._handle;
1 change: 1 addition & 0 deletions lib/net.js
Original file line number Diff line number Diff line change
@@ -1343,6 +1343,7 @@ function listenInCluster(server, address, port, addressType,
addressType: addressType,
fd: fd,
flags,
backlog,
};

// Get the master's server handle, and listen on it
40 changes: 40 additions & 0 deletions test/parallel/test-cluster-net-listen-backlog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

const common = require('../common');
const assert = require('assert');
// Monkey-patch `net.Server.listen`
const net = require('net');
const cluster = require('cluster');

// Ensures that the `backlog` is used to create a `net.Server`.
const kExpectedBacklog = 127;
if (cluster.isMaster) {
const listen = net.Server.prototype.listen;

net.Server.prototype.listen = common.mustCall(
function(...args) {
const options = args[0];
if (typeof options === 'object') {
assert(options.backlog, kExpectedBacklog);
} else {
assert(args[1], kExpectedBacklog);
}
return listen.call(this, ...args);
}
);

const worker = cluster.fork();
worker.on('message', () => {
worker.disconnect();
});
} else {
const server = net.createServer();

server.listen({
host: common.localhostIPv4,
port: 0,
backlog: kExpectedBacklog,
}, common.mustCall(() => {
process.send(true);
}));
}