mirror of
https://github.com/zebrajr/node.git
synced 2025-12-06 12:20:27 +01:00
lib: make sure close net server
PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
parent
3a456c6db8
commit
29ec7e9331
15
lib/net.js
15
lib/net.js
|
|
@ -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;
|
||||
|
|
@ -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) {
|
||||
|
|
@ -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 {
|
||||
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -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
test/parallel/test-net-server-close-before-ipc-response.js
Normal file
22
test/parallel/test-net-server-close-before-ipc-response.js
Normal file
|
|
@ -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();
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user