mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
net: fix 'close' event emit order
The server 'close' event was emitted before the last client 'close' event. Not exactly fatal but potentially confusing. Before this commit the order looked something like [client, server, client], now it looks like [client, client, server]. See #3340 for more details.
This commit is contained in:
10
lib/net.js
10
lib/net.js
@@ -342,11 +342,6 @@ Socket.prototype._destroy = function(exception, cb) {
|
||||
|
||||
timers.unenroll(this);
|
||||
|
||||
if (this.server) {
|
||||
this.server._connections--;
|
||||
this.server._emitCloseIfDrained();
|
||||
}
|
||||
|
||||
debug('close');
|
||||
if (this._handle) {
|
||||
this._handle.close();
|
||||
@@ -361,6 +356,11 @@ Socket.prototype._destroy = function(exception, cb) {
|
||||
});
|
||||
|
||||
this.destroyed = true;
|
||||
|
||||
if (this.server) {
|
||||
this.server._connections--;
|
||||
this.server._emitCloseIfDrained();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -19,22 +19,36 @@
|
||||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
|
||||
|
||||
|
||||
var common = require('../common');
|
||||
var assert = require('assert');
|
||||
var net = require('net');
|
||||
|
||||
var server = net.createServer(function(socket) {
|
||||
server.close(function() {
|
||||
assert.equal(server.connections, 0);
|
||||
});
|
||||
process.nextTick(function() {
|
||||
socket.destroy();
|
||||
var events = [];
|
||||
var sockets = [];
|
||||
|
||||
process.on('exit', function() {
|
||||
assert.equal(server.connections, 0);
|
||||
assert.deepEqual(events, 'client client server'.split(' '));
|
||||
});
|
||||
|
||||
var server = net.createServer(function(c) {
|
||||
c.on('close', function() {
|
||||
events.push('client');
|
||||
});
|
||||
|
||||
sockets.push(c);
|
||||
|
||||
if (sockets.length === 2) {
|
||||
server.close();
|
||||
sockets.forEach(function(c) { c.destroy() });
|
||||
}
|
||||
});
|
||||
|
||||
server.on('close', function() {
|
||||
events.push('server');
|
||||
});
|
||||
|
||||
server.listen(common.PORT, function() {
|
||||
net.createConnection(common.PORT);
|
||||
});
|
||||
net.createConnection(common.PORT);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user