Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4d99797

Browse filesBrowse files
theanarkhtargos
authored andcommitted
lib: make sure close net server
PR-URL: #51929 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io> Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
1 parent 9617adc commit 4d99797
Copy full SHA for 4d99797

File tree

Expand file treeCollapse file tree

3 files changed

+42
-2
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+42
-2
lines changed
Open diff view settings
Collapse file

‎lib/net.js‎

Copy file name to clipboardExpand all lines: lib/net.js
+13-2Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,6 +1773,7 @@ function Server(options, connectionListener) {
17731773
this._usingWorkers = false;
17741774
this._workers = [];
17751775
this._unref = false;
1776+
this._listeningId = 1;
17761777

17771778
this.allowHalfOpen = options.allowHalfOpen || false;
17781779
this.pauseOnConnect = !!options.pauseOnConnect;
@@ -1954,10 +1955,14 @@ function listenInCluster(server, address, port, addressType,
19541955
backlog,
19551956
...options,
19561957
};
1958+
const listeningId = server._listeningId;
19571959
// Get the primary's server handle, and listen on it
19581960
cluster._getServer(server, serverQuery, listenOnPrimaryHandle);
1959-
19601961
function listenOnPrimaryHandle(err, handle) {
1962+
if (listeningId !== server._listeningId) {
1963+
handle.close();
1964+
return;
1965+
}
19611966
err = checkBindError(err, port, handle);
19621967

19631968
if (err) {
@@ -2089,9 +2094,14 @@ Server.prototype.listen = function(...args) {
20892094
throw new ERR_INVALID_ARG_VALUE('options', options);
20902095
};
20912096

2092-
function lookupAndListen(self, port, address, backlog, exclusive, flags) {
2097+
function lookupAndListen(self, port, address, backlog,
2098+
exclusive, flags) {
20932099
if (dns === undefined) dns = require('dns');
2100+
const listeningId = self._listeningId;
20942101
dns.lookup(address, function doListen(err, ip, addressType) {
2102+
if (listeningId !== self._listeningId) {
2103+
return;
2104+
}
20952105
if (err) {
20962106
self.emit('error', err);
20972107
} else {
@@ -2237,6 +2247,7 @@ Server.prototype.getConnections = function(cb) {
22372247

22382248

22392249
Server.prototype.close = function(cb) {
2250+
this._listeningId++;
22402251
if (typeof cb === 'function') {
22412252
if (!this._handle) {
22422253
this.once('close', function close() {
Collapse file
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const net = require('net');
5+
// Process should exit because it does not create a real TCP server.
6+
// Paas localhost to ensure create TCP handle asynchronously because It causes DNS resolution.
7+
net.createServer().listen(0, 'localhost', common.mustNotCall()).close();
Collapse file
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const net = require('net');
5+
const cluster = require('cluster');
6+
7+
// Process should exit
8+
if (cluster.isPrimary) {
9+
cluster.fork();
10+
} else {
11+
const send = process.send;
12+
process.send = function(message) {
13+
// listenOnPrimaryHandle in net.js should call handle.close()
14+
if (message.act === 'close') {
15+
setImmediate(() => {
16+
process.disconnect();
17+
});
18+
}
19+
return send.apply(this, arguments);
20+
};
21+
net.createServer().listen(0, common.mustNotCall()).close();
22+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.