From c24276f0087a5f72f68be2384f32e20ba83dfa09 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Thu, 29 Dec 2011 19:30:07 +0100 Subject: [PATCH] net: defer net.Server 'close' event to next tick --- lib/net.js | 10 +++++++--- test/simple/test-net-server-listen-remove-callback.js | 3 +++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/net.js b/lib/net.js index 072896b4c4e..12621862bba 100644 --- a/lib/net.js +++ b/lib/net.js @@ -862,9 +862,13 @@ Server.prototype.close = function() { }; Server.prototype._emitCloseIfDrained = function() { - if (!this._handle && !this.connections) { - this.emit('close'); - } + var self = this; + + if (self._handle || self.connections) return; + + process.nextTick(function() { + self.emit('close'); + }); }; diff --git a/test/simple/test-net-server-listen-remove-callback.js b/test/simple/test-net-server-listen-remove-callback.js index a92a41859b5..e10231de8cd 100644 --- a/test/simple/test-net-server-listen-remove-callback.js +++ b/test/simple/test-net-server-listen-remove-callback.js @@ -34,6 +34,9 @@ server.on('close', function() { server.listen(common.PORT, function() { server.close(); +}); + +server.once('close', function() { server.listen(common.PORT + 1, function() { server.close(); });