GeezerVison is showing github.com — lib: make sure close net server · nodejs/node@4d99797
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/net.js +13-2Lines changed: 13 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1773,6 +1773,7 @@ function Server(options, connectionListener) { | 1773 | 1773 | this._usingWorkers = false; | 1774 | 1774 | this._workers = []; | 1775 | 1775 | this._unref = false; | | 1776 | + this._listeningId = 1; | 1776 | 1777 |
| 1777 | 1778 | this.allowHalfOpen = options.allowHalfOpen || false; | 1778 | 1779 | this.pauseOnConnect = !!options.pauseOnConnect; | @@ -1954,10 +1955,14 @@ function listenInCluster(server, address, port, addressType, | 1954 | 1955 | backlog, | 1955 | 1956 | ...options, | 1956 | 1957 | }; | | 1958 | + const listeningId = server._listeningId; | 1957 | 1959 | // Get the primary's server handle, and listen on it | 1958 | 1960 | cluster._getServer(server, serverQuery, listenOnPrimaryHandle); | 1959 | | - | 1960 | 1961 | function listenOnPrimaryHandle(err, handle) { | | 1962 | + if (listeningId !== server._listeningId) { | | 1963 | + handle.close(); | | 1964 | + return; | | 1965 | + } | 1961 | 1966 | err = checkBindError(err, port, handle); | 1962 | 1967 |
| 1963 | 1968 | if (err) { | @@ -2089,9 +2094,14 @@ Server.prototype.listen = function(...args) { | 2089 | 2094 | throw new ERR_INVALID_ARG_VALUE('options', options); | 2090 | 2095 | }; | 2091 | 2096 |
| 2092 | | -function lookupAndListen(self, port, address, backlog, exclusive, flags) { | | 2097 | +function lookupAndListen(self, port, address, backlog, | | 2098 | + exclusive, flags) { | 2093 | 2099 | if (dns === undefined) dns = require('dns'); | | 2100 | + const listeningId = self._listeningId; | 2094 | 2101 | dns.lookup(address, function doListen(err, ip, addressType) { | | 2102 | + if (listeningId !== self._listeningId) { | | 2103 | + return; | | 2104 | + } | 2095 | 2105 | if (err) { | 2096 | 2106 | self.emit('error', err); | 2097 | 2107 | } else { | @@ -2237,6 +2247,7 @@ Server.prototype.getConnections = function(cb) { | 2237 | 2247 |
| 2238 | 2248 |
| 2239 | 2249 | Server.prototype.close = function(cb) { | | 2250 | + this._listeningId++; | 2240 | 2251 | if (typeof cb === 'function') { | 2241 | 2252 | if (!this._handle) { | 2242 | 2253 | this.once('close', function close() { | |
Collapse file Copy file name to clipboard +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 Copy file name to clipboard +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +} |
|