Skip to content

Commit 14de082

Browse files
authored
lib: emit listening event once when call listen twice
PR-URL: #52119 Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 5276c0d commit 14de082

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

lib/net.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1970,7 +1970,11 @@ function listenInCluster(server, address, port, addressType,
19701970
const ex = new ExceptionWithHostPort(err, 'bind', address, port);
19711971
return server.emit('error', ex);
19721972
}
1973-
1973+
// If there was a handle, just close it to avoid fd leak
1974+
// but it doesn't look like that's going to happen right now
1975+
if (server._handle) {
1976+
server._handle.close();
1977+
}
19741978
// Reuse primary's server handle
19751979
server._handle = handle;
19761980
// _listen2 sets up the listened handle, it is still named like this
@@ -1999,6 +2003,8 @@ Server.prototype.listen = function(...args) {
19992003

20002004
options = options._handle || options.handle || options;
20012005
const flags = getFlags(options.ipv6Only);
2006+
// Refresh the id to make the previous call invalid
2007+
this._listeningId++;
20022008
// (handle[, backlog][, cb]) where handle is an object with a handle
20032009
if (options instanceof TCP) {
20042010
this._handle = options;
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
const common = require('../common');
3+
const net = require('net');
4+
const cluster = require('cluster');
5+
const assert = require('assert');
6+
7+
if (cluster.isPrimary) {
8+
const worker = cluster.fork();
9+
worker.on('exit', common.mustCall((code) => {
10+
assert.ok(code === 0);
11+
}));
12+
} else {
13+
const server = net.createServer();
14+
server.listen();
15+
try {
16+
// Currently, we can call `listen` twice in cluster worker,
17+
// if we can not call `listen` twice in the futrue,
18+
// just skip this test.
19+
server.listen();
20+
} catch (e) {
21+
console.error(e);
22+
process.exit(0);
23+
}
24+
let i = 0;
25+
process.on('internalMessage', (msg) => {
26+
if (msg.cmd === 'NODE_CLUSTER') {
27+
if (++i === 2) {
28+
setImmediate(() => {
29+
server.close(() => {
30+
process.disconnect();
31+
});
32+
});
33+
}
34+
}
35+
});
36+
// Must only call once
37+
server.on('listening', common.mustCall());
38+
}

0 commit comments

Comments
 (0)