Skip to content

Commit f209160

Browse files
committed
fix(replset): don't leak servers failing to connect
The change to include all connecting servers in the local `connectingServers` array lead to a "server leak" for nodes that we are not able to connect to. Now we track them, and only add new ones on a successful reconnect attempt. NODE-2270
1 parent 295ea4c commit f209160

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

lib/core/topologies/replset.js

+25-9
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,26 @@ function rexecuteOperations(self) {
277277
}
278278

279279
function connectNewServers(self, servers, callback) {
280+
// No new servers
281+
if (servers.length === 0) {
282+
return callback();
283+
}
284+
280285
// Count lefts
281286
var count = servers.length;
282287
var error = null;
283288

289+
function done() {
290+
count = count - 1;
291+
if (count === 0) {
292+
callback(error);
293+
}
294+
}
295+
284296
// Handle events
285297
var _handleEvent = function(self, event) {
286298
return function(err) {
287299
var _self = this;
288-
count = count - 1;
289300

290301
// Destroyed
291302
if (self.state === DESTROYED || self.state === UNREFERENCED) {
@@ -332,17 +343,10 @@ function connectNewServers(self, servers, callback) {
332343

333344
// Rexecute any stalled operation
334345
rexecuteOperations(self);
335-
336-
// Are we done finish up callback
337-
if (count === 0) {
338-
callback(error);
339-
}
346+
done();
340347
};
341348
};
342349

343-
// No new servers
344-
if (count === 0) return callback();
345-
346350
// Execute method
347351
function execute(_server, i) {
348352
setTimeout(function() {
@@ -351,6 +355,18 @@ function connectNewServers(self, servers, callback) {
351355
return;
352356
}
353357

358+
// remove existing connecting server if it's failed to connect, otherwise
359+
// wait for that server to connect
360+
const existingServerIdx = self.s.connectingServers.findIndex(s => s.name === _server);
361+
if (existingServerIdx >= 0) {
362+
const connectingServer = self.s.connectingServers[existingServerIdx];
363+
connectingServer.destroy({ force: true });
364+
365+
self.s.connectingServers.splice(existingServerIdx, 1);
366+
done();
367+
return;
368+
}
369+
354370
// Create a new server instance
355371
var server = new Server(
356372
Object.assign({}, self.s.options, {

0 commit comments

Comments
 (0)