Skip to content

Commit

Permalink
lib: emit listening event once when call listen twice
Browse files Browse the repository at this point in the history
  • Loading branch information
theanarkh committed Mar 18, 2024
1 parent 639c096 commit dae3f7e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
7 changes: 5 additions & 2 deletions lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,8 @@ function setupChannel(target, channel, serializationMode) {
const obj = handleConversion[message.type];

// Update simultaneous accepts on Windows
if (process.platform === 'win32') {
if (process.platform === 'win32' &&
typeof handle.setSimultaneousAccepts === 'function') {
handle.setSimultaneousAccepts(false);
}

Expand Down Expand Up @@ -831,7 +832,9 @@ function setupChannel(target, channel, serializationMode) {
message = message.msg;

// Update simultaneous accepts on Windows
if (obj.simultaneousAccepts && process.platform === 'win32') {
if (obj.simultaneousAccepts &&
process.platform === 'win32' &&
typeof handle.setSimultaneousAccepts === 'function') {
handle.setSimultaneousAccepts(true);
}
} else if (this._handleQueue &&
Expand Down
8 changes: 7 additions & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1969,7 +1969,11 @@ function listenInCluster(server, address, port, addressType,
const ex = new ExceptionWithHostPort(err, 'bind', address, port);
return server.emit('error', ex);
}

// If there was a handle, just close it to avoid fd leak
// but it doesn't look like that's going to happen right now
if (server._handle) {
server._handle.close();
}
// Reuse primary's server handle
server._handle = handle;
// _listen2 sets up the listened handle, it is still named like this
Expand Down Expand Up @@ -1998,6 +2002,8 @@ Server.prototype.listen = function(...args) {

options = options._handle || options.handle || options;
const flags = getFlags(options.ipv6Only);
// Refresh the id to make the previous call invalid
this._listeningId++;
// (handle[, backlog][, cb]) where handle is an object with a handle
if (options instanceof TCP) {
this._handle = options;
Expand Down
38 changes: 38 additions & 0 deletions test/parallel/test-net-listen-twice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';
const common = require('../common');
const net = require('net');
const cluster = require('cluster');
const assert = require('assert');

if (cluster.isPrimary) {
const worker = cluster.fork();
worker.on('exit', common.mustCall((code) => {
assert.ok(code === 0);
}));
} else {
const server = net.createServer();
server.listen();
try {
// Currently, we can call `listen` twice in cluster worker,
// if we can not call `listen` twice in the futrue,
// just skip this test.
server.listen();
} catch (e) {
console.error(e);
process.exit(0);
}
let i = 0;
process.on('internalMessage', (msg) => {
if (msg.cmd === 'NODE_CLUSTER') {
if (++i === 2) {
setImmediate(() => {
server.close(() => {
process.disconnect();
});
});
}
}
});
// Must only call once
server.on('listening', common.mustCall());
}

0 comments on commit dae3f7e

Please sign in to comment.