Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Emit 'close' after all connections have closed
Browse files Browse the repository at this point in the history
Fixes #1383
  • Loading branch information
felixge committed Jul 26, 2011
1 parent df3a8fc commit 09ee293
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 @@ -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
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion lib/net_uv.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,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 @@ -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');

This comment has been minimized.

Copy link
@bnoordhuis

bnoordhuis Dec 28, 2011

Member

Judgment call: should the event be deferred to the next tick? It's somewhat inconsistent now with how other events are dispatched. See #2418 for details.

This comment has been minimized.

Copy link
@koichik

koichik Dec 28, 2011

+1 to deferred.

This comment has been minimized.

Copy link
@bnoordhuis

bnoordhuis Dec 30, 2011

Member

Fixed in c24276f.

}
};


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 09ee293

Please sign in to comment.