Skip to content

Commit ae3fc20

Browse files
committed
lib: emit listening event once when call listen twice
1 parent 639c096 commit ae3fc20

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

lib/net.js

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

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

0 commit comments

Comments
 (0)