From ca3519b08613f3250b7b639bd2e7f9e0b5740090 Mon Sep 17 00:00:00 2001 From: theanarkh Date: Tue, 12 Mar 2024 22:29:15 +0800 Subject: [PATCH] lib: fix listen with handle in cluster worker --- lib/net.js | 2 +- .../test-net-listen-handle-in-cluster-1.js | 27 +++++++++++++++++++ .../test-net-listen-handle-in-cluster-2.js | 21 +++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-net-listen-handle-in-cluster-1.js create mode 100644 test/parallel/test-net-listen-handle-in-cluster-2.js diff --git a/lib/net.js b/lib/net.js index fa470dbb060455..548708bc4e06df 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1994,7 +1994,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); diff --git a/test/parallel/test-net-listen-handle-in-cluster-1.js b/test/parallel/test-net-listen-handle-in-cluster-1.js new file mode 100644 index 00000000000000..07e002bf2aaebb --- /dev/null +++ b/test/parallel/test-net-listen-handle-in-cluster-1.js @@ -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); + })); +} diff --git a/test/parallel/test-net-listen-handle-in-cluster-2.js b/test/parallel/test-net-listen-handle-in-cluster-2.js new file mode 100644 index 00000000000000..33d6642e9b9386 --- /dev/null +++ b/test/parallel/test-net-listen-handle-in-cluster-2.js @@ -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); + })); +}