From 09ee29318f32fdbe68d04188795bb6c760f4835c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Geisendo=CC=88rfer?= Date: Mon, 25 Jul 2011 09:01:44 +0200 Subject: [PATCH] Emit 'close' after all connections have closed Fixes #1383 --- lib/net_legacy.js | 10 +++++-- lib/net_uv.js | 9 ++++++- test/simple/test-net-server-close.js | 39 ++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 test/simple/test-net-server-close.js diff --git a/lib/net_legacy.js b/lib/net_legacy.js index 1a383325136..4221aa11461 100644 --- a/lib/net_legacy.js +++ b/lib/net_legacy.js @@ -832,6 +832,7 @@ Socket.prototype.destroy = function(exception) { if (this.server && !this.destroyed) { this.server.connections--; + this.server._emitCloseIfDrained(); } // FIXME Bug when this.fd == 0 @@ -1159,13 +1160,18 @@ Server.prototype.close = function() { if (self.type === 'unix') { require('fs').unlink(self.path, function() { - self.emit('close'); + self._emitCloseIfDrained(); }); } else { - self.emit('close'); + self._emitCloseIfDrained(); } }; +Server.prototype._emitCloseIfDrained = function() { + if ((typeof this.fd !== 'number') && !this.connections) { + this.emit('close'); + } +}; var dummyFD = null; var lastEMFILEWarning = 0; diff --git a/lib/net_uv.js b/lib/net_uv.js index e87b90529d2..b9cafaee266 100644 --- a/lib/net_uv.js +++ b/lib/net_uv.js @@ -216,6 +216,7 @@ Socket.prototype.destroy = function(exception) { if (this.server && !this.destroyed) { this.server.connections--; + this.server._emitCloseIfDrained(); } debug('close ' + this.fd); @@ -647,7 +648,13 @@ Server.prototype.close = function() { this._handle.close(); this._handle = null; - this.emit('close'); + this._emitCloseIfDrained(); +}; + +Server.prototype._emitCloseIfDrained = function() { + if (!this._handle && !this.connections) { + this.emit('close'); + } }; diff --git a/test/simple/test-net-server-close.js b/test/simple/test-net-server-close.js new file mode 100644 index 00000000000..08f11194146 --- /dev/null +++ b/test/simple/test-net-server-close.js @@ -0,0 +1,39 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// 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(); + process.nextTick(function() { + socket.destroy(); + }); +}); + +server.listen(common.PORT, function() { + net.createConnection(common.PORT); +}); + +server.on('close', function() { + assert.equal(server.connections, 0); +});