diff --git a/lib/connection/pool.js b/lib/connection/pool.js index fe948e3c8..2a3f5d61d 100644 --- a/lib/connection/pool.js +++ b/lib/connection/pool.js @@ -217,6 +217,12 @@ Pool.prototype.capConnections = function(maxConnections) { var connections = this.connections.slice(maxConnections); // Cap the active connections this.connections = this.connections.slice(0, maxConnections); + + if (this.index >= maxConnections){ + // Go back to the beggining of the pool if capping connections + this.index = 0; + } + // Remove all listeners for(var i = 0; i < connections.length; i++) { connections[i].removeAllListeners('close'); diff --git a/lib/topologies/server.js b/lib/topologies/server.js index b5e7e82e4..fd8d16151 100644 --- a/lib/topologies/server.js +++ b/lib/topologies/server.js @@ -699,7 +699,8 @@ var executeSingleOperation = function(self, ns, cmd, queryOptions, options, onAl var connection = options.connection || self.s.pool.get(); // Double check if we have a valid connection - if(!connection.isConnected()) { + // Checking that the connection exists to avoid an uncaught exception in case there is an issue with the pool + if(!(connection && connection.isConnected())) { return callback(new MongoError(f("no connection available to server %s", self.name))); }