0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00

Emit 'close' after all connections have closed

Fixes #1383
This commit is contained in:
Felix Geisendörfer 2011-07-25 09:01:44 +02:00
parent df3a8fcb62
commit 09ee29318f
3 changed files with 55 additions and 3 deletions

View File

@ -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;

View File

@ -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');
}
};

View File

@ -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);
});