From fa378ee4d8c431ac1312b8a1dd12681d59d8e1c3 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Tue, 9 Aug 2011 16:26:42 +0200 Subject: [PATCH] net: defer DNS lookup error events to next tick 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 | 8 +++++++- lib/net_uv.js | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/net_legacy.js b/lib/net_legacy.js index 2abe2cde7a..7f24e6b39c 100644 --- a/lib/net_legacy.js +++ b/lib/net_legacy.js @@ -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; diff --git a/lib/net_uv.js b/lib/net_uv.js index bd79b2459c..4476823873 100644 --- a/lib/net_uv.js +++ b/lib/net_uv.js @@ -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);