net: defer net.Server 'close' event to next tick
authorBen Noordhuis <info@bnoordhuis.nl>
Thu, 29 Dec 2011 18:30:07 +0000 (19:30 +0100)
committerBen Noordhuis <info@bnoordhuis.nl>
Thu, 29 Dec 2011 18:30:07 +0000 (19:30 +0100)
lib/net.js
test/simple/test-net-server-listen-remove-callback.js

index 072896b..1262186 100644 (file)
@@ -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');
+  });
 };
 
 
index a92a418..e10231d 100644 (file)
@@ -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();
   });