Skip to content

Commit

Permalink
lib: make sure close net server
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
theanarkh authored and richardlau committed Mar 25, 2024
1 parent 0a7419b commit 44f05e0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1773,6 +1773,7 @@ function Server(options, connectionListener) {
this._usingWorkers = false;
this._workers = [];
this._unref = false;
this._listeningId = 1;

this.allowHalfOpen = options.allowHalfOpen || false;
this.pauseOnConnect = !!options.pauseOnConnect;
Expand Down Expand Up @@ -1954,10 +1955,14 @@ function listenInCluster(server, address, port, addressType,
backlog,
...options,
};
const listeningId = server._listeningId;
// Get the primary's server handle, and listen on it
cluster._getServer(server, serverQuery, listenOnPrimaryHandle);

function listenOnPrimaryHandle(err, handle) {
if (listeningId !== server._listeningId) {
handle.close();
return;
}
err = checkBindError(err, port, handle);

if (err) {
Expand Down Expand Up @@ -2089,9 +2094,14 @@ Server.prototype.listen = function(...args) {
throw new ERR_INVALID_ARG_VALUE('options', options);
};

function lookupAndListen(self, port, address, backlog, exclusive, flags) {
function lookupAndListen(self, port, address, backlog,
exclusive, flags) {
if (dns === undefined) dns = require('dns');
const listeningId = self._listeningId;
dns.lookup(address, function doListen(err, ip, addressType) {
if (listeningId !== self._listeningId) {
return;
}
if (err) {
self.emit('error', err);
} else {
Expand Down Expand Up @@ -2237,6 +2247,7 @@ Server.prototype.getConnections = function(cb) {


Server.prototype.close = function(cb) {
this._listeningId++;
if (typeof cb === 'function') {
if (!this._handle) {
this.once('close', function close() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

const common = require('../common');
const net = require('net');
// Process should exit because it does not create a real TCP server.
// Paas localhost to ensure create TCP handle asynchronously because It causes DNS resolution.
net.createServer().listen(0, 'localhost', common.mustNotCall()).close();
22 changes: 22 additions & 0 deletions test/parallel/test-net-server-close-before-ipc-response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const common = require('../common');
const net = require('net');
const cluster = require('cluster');

// Process should exit
if (cluster.isPrimary) {
cluster.fork();
} else {
const send = process.send;
process.send = function(message) {
// listenOnPrimaryHandle in net.js should call handle.close()
if (message.act === 'close') {
setImmediate(() => {
process.disconnect();
});
}
return send.apply(this, arguments);
};
net.createServer().listen(0, common.mustNotCall()).close();
}

0 comments on commit 44f05e0

Please sign in to comment.