diff --git a/lib/client.js b/lib/client.js index 490e1d183..73f910a20 100644 --- a/lib/client.js +++ b/lib/client.js @@ -170,9 +170,17 @@ Client.prototype.connect = function(callback) { return self.emit('error', error); } callback(error); + callback = null; }); con.once('end', function() { + if ( callback ) { + // haven't received a connection message yet ! + var err = new Error("Stream unexpectedly ended before getting ready for query execution"); + callback(err); + callback = null; + return; + } if(self.activeQuery) { var disconnectError = new Error('Stream unexpectedly ended during query execution'); self.activeQuery.handleError(disconnectError); diff --git a/lib/pool.js b/lib/pool.js index 9cf9aabf0..887a0e4ed 100644 --- a/lib/pool.js +++ b/lib/pool.js @@ -33,10 +33,20 @@ var pools = { pool.destroy(client); }); + // Remove connection from pool on disconnect + client.on('end', function(e) { + // Do not enter infinite loop between pool.destroy + // and client 'end' event... + if ( ! client._destroying ) { + pool.destroy(client); + } + }); + return cb(null, client); }); }, destroy: function(client) { + client._destroying = true; client.end(); } });