Browse Source

Emit 'close' after all connections have closed

Fixes #1383
v0.7.4-release
Felix Geisendörfer 13 years ago
parent
commit
09ee29318f
  1. 10
      lib/net_legacy.js
  2. 9
      lib/net_uv.js
  3. 39
      test/simple/test-net-server-close.js

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

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

39
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);
});
Loading…
Cancel
Save