Skip to content

Commit

Permalink
Emit 'close' after all connections have closed
Browse files Browse the repository at this point in the history
  • Loading branch information
felixge committed Jul 25, 2011
1 parent c050d0f commit d038be2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lib/net_legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,7 @@ Socket.prototype.destroy = function(exception) {

if (this.server && !this.destroyed) {
this.server.connections--;
this.server._emitCloseIfDrained();
}

// FIXME Bug when this.fd == 0
Expand Down Expand Up @@ -1153,13 +1154,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;
Expand Down
9 changes: 8 additions & 1 deletion lib/net_uv.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ Socket.prototype.destroy = function(exception) {

if (this.server && !this.destroyed) {
this.server.connections--;
this.server._emitCloseIfDrained();
}

debug('close ' + this.fd);
Expand Down Expand Up @@ -641,7 +642,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');
}
};


Expand Down
39 changes: 39 additions & 0 deletions test/simple/test-net-server-close.js
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit d038be2

Please sign in to comment.