Skip to content

Commit

Permalink
lib: fix listen with handle in cluster worker
Browse files Browse the repository at this point in the history
PR-URL: nodejs#52056
Reviewed-By: Paolo Insogna <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
  • Loading branch information
theanarkh authored and rdw-msft committed Mar 26, 2024
1 parent 60bf772 commit 0ed2abb
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -2002,7 +2002,7 @@ Server.prototype.listen = function(...args) {
if (options instanceof TCP) {
this._handle = options;
this[async_id_symbol] = this._handle.getAsyncId();
listenInCluster(this, null, -1, -1, backlogFromArgs);
listenInCluster(this, null, -1, -1, backlogFromArgs, undefined, true);
return this;
}
addServerAbortSignalOption(this, options);
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-net-listen-handle-in-cluster-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');
const cluster = require('cluster');

// Test if the worker can listen with handle successfully
if (cluster.isPrimary) {
const worker = cluster.fork();
const server = net.createServer();
worker.on('online', common.mustCall(() => {
server.listen(common.mustCall(() => {
// Send the server to worker
worker.send(null, server);
}));
}));
worker.on('exit', common.mustCall(() => {
server.close();
}));
} else {
// The `got` function of net.Server will create a TCP server by listen(handle)
// See lib/internal/child_process.js
process.on('message', common.mustCall((_, server) => {
assert.strictEqual(server instanceof net.Server, true);
process.exit(0);
}));
}
21 changes: 21 additions & 0 deletions test/parallel/test-net-listen-handle-in-cluster-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Flags: --expose-internals
'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');
const cluster = require('cluster');
const { internalBinding } = require('internal/test/binding');
const { TCP, constants: TCPConstants } = internalBinding('tcp_wrap');

// Test if the worker can listen with handle successfully
if (cluster.isPrimary) {
cluster.fork();
} else {
const handle = new TCP(TCPConstants.SOCKET);
const errno = handle.bind('0.0.0.0', 0);
assert.strictEqual(errno, 0);
// Execute _listen2 instead of cluster._getServer in listenInCluster
net.createServer().listen(handle, common.mustCall(() => {
process.exit(0);
}));
}

0 comments on commit 0ed2abb

Please sign in to comment.