Skip to content

Commit ea4905c

Browse files
theanarkhmarco-ippolito
authored andcommittedMay 3, 2024
lib: fix listen with handle in cluster worker
PR-URL: #52056 Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 51d7cd5 commit ea4905c

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed
 

‎lib/net.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2002,7 +2002,7 @@ Server.prototype.listen = function(...args) {
20022002
if (options instanceof TCP) {
20032003
this._handle = options;
20042004
this[async_id_symbol] = this._handle.getAsyncId();
2005-
listenInCluster(this, null, -1, -1, backlogFromArgs);
2005+
listenInCluster(this, null, -1, -1, backlogFromArgs, undefined, true);
20062006
return this;
20072007
}
20082008
addServerAbortSignalOption(this, options);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const net = require('net');
5+
const cluster = require('cluster');
6+
7+
// Test if the worker can listen with handle successfully
8+
if (cluster.isPrimary) {
9+
const worker = cluster.fork();
10+
const server = net.createServer();
11+
worker.on('online', common.mustCall(() => {
12+
server.listen(common.mustCall(() => {
13+
// Send the server to worker
14+
worker.send(null, server);
15+
}));
16+
}));
17+
worker.on('exit', common.mustCall(() => {
18+
server.close();
19+
}));
20+
} else {
21+
// The `got` function of net.Server will create a TCP server by listen(handle)
22+
// See lib/internal/child_process.js
23+
process.on('message', common.mustCall((_, server) => {
24+
assert.strictEqual(server instanceof net.Server, true);
25+
process.exit(0);
26+
}));
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const net = require('net');
6+
const cluster = require('cluster');
7+
const { internalBinding } = require('internal/test/binding');
8+
const { TCP, constants: TCPConstants } = internalBinding('tcp_wrap');
9+
10+
// Test if the worker can listen with handle successfully
11+
if (cluster.isPrimary) {
12+
cluster.fork();
13+
} else {
14+
const handle = new TCP(TCPConstants.SOCKET);
15+
const errno = handle.bind('0.0.0.0', 0);
16+
assert.strictEqual(errno, 0);
17+
// Execute _listen2 instead of cluster._getServer in listenInCluster
18+
net.createServer().listen(handle, common.mustCall(() => {
19+
process.exit(0);
20+
}));
21+
}

0 commit comments

Comments
 (0)
Please sign in to comment.