net: defer DNS lookup error events to next tick
authorBen Noordhuis <info@bnoordhuis.nl>
Tue, 9 Aug 2011 14:26:42 +0000 (16:26 +0200)
committerBen Noordhuis <info@bnoordhuis.nl>
Fri, 12 Aug 2011 13:22:56 +0000 (15:22 +0200)
net.createConnection() creates a net.Socket object
and immediately calls net.Socket.connect() on it.

There are no event listeners registered yet so
defer the error event to the next tick.

Fixes #1202.

lib/net_legacy.js
lib/net_uv.js

index 2abe2cde7a2563ed26bd840d8c28b473398aebdd..7f24e6b39cfee51be1565056d25633bc66171791 100644 (file)
@@ -728,7 +728,13 @@ Socket.prototype.connect = function() {
     // TCP
     require('dns').lookup(host, function(err, ip, addressType) {
       if (err) {
-        self.emit('error', err);
+        // net.createConnection() creates a net.Socket object and
+        // immediately calls net.Socket.connect() on it (that's us).
+        // There are no event listeners registered yet so defer the
+        // error event to the next tick.
+        process.nextTick(function() {
+          self.emit('error', err);
+        });
       } else {
         addressType = addressType || 4;
 
index bd79b2459ccb404e26ae7010aea559c82fc4b108..44768238736d0fc234e63325327352b946642108 100644 (file)
@@ -469,7 +469,13 @@ Socket.prototype.connect = function(port /* [host], [cb] */) {
     debug("connect: find host " + host);
     require('dns').lookup(host, function(err, ip, addressType) {
       if (err) {
-        self.emit('error', err);
+        // net.createConnection() creates a net.Socket object and
+        // immediately calls net.Socket.connect() on it (that's us).
+        // There are no event listeners registered yet so defer the
+        // error event to the next tick.
+        process.nextTick(function() {
+          self.emit('error', err);
+        });
       } else {
         timers.active(self);