Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
fix(mongos): fix connection leak when mongos reconnects
Browse files Browse the repository at this point in the history
Our mongos reconnect logic involved creating a new Server+Pool
per proxy per iteration of the reconnect logic. These instances
were never cleaned up, resulting in a huge number of connections
when the proxies finally reconnected.

Fixes NODE-1403
  • Loading branch information
daprahamian authored and mbroadst committed Aug 13, 2018
1 parent e9bef43 commit 2453746
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/topologies/mongos.js
Expand Up @@ -568,13 +568,13 @@ function reconnectProxies(self, proxies, callback) {
_self.on('parseError', handleEvent(self, 'parseError'));

// Move to the connected servers
moveServerFrom(self.disconnectedProxies, self.connectedProxies, _self);
moveServerFrom(self.connectingProxies, self.connectedProxies, _self);
// Emit topology Change
emitTopologyDescriptionChanged(self);
// Emit joined event
self.emit('joined', 'mongos', _self);
});
} else if (event === 'connect' && self.authenticating) {
} else {
// Move from connectingProxies
moveServerFrom(self.connectingProxies, self.disconnectedProxies, _self);
this.destroy();
Expand Down Expand Up @@ -613,6 +613,9 @@ function reconnectProxies(self, proxies, callback) {
})
);

_server.destroy();
removeProxyFrom(self.disconnectedProxies, _server);

// Relay the server description change
server.on('serverDescriptionChanged', function(event) {
self.emit('serverDescriptionChanged', event);
Expand All @@ -635,6 +638,7 @@ function reconnectProxies(self, proxies, callback) {
relayEvents(server, self, ['commandStarted', 'commandSucceeded', 'commandFailed']);

// Connect to proxy
self.connectingProxies.push(server);
server.connect(self.s.connectOptions);
}, i);
}
Expand Down
8 changes: 7 additions & 1 deletion lib/topologies/server.js
Expand Up @@ -1001,6 +1001,8 @@ var listeners = ['close', 'error', 'timeout', 'parseError', 'connect'];
* @param {boolean} [options.force=false] Force destroy the pool
*/
Server.prototype.destroy = function(options) {
if (this._destroyed) return;

options = options || {};
var self = this;

Expand All @@ -1013,7 +1015,10 @@ Server.prototype.destroy = function(options) {
}

// No pool, return
if (!self.s.pool) return;
if (!self.s.pool) {
this._destroyed = true;
return;
}

// Emit close event
if (options.emitClose) {
Expand Down Expand Up @@ -1048,6 +1053,7 @@ Server.prototype.destroy = function(options) {

// Destroy the pool
this.s.pool.destroy(options.force);
this._destroyed = true;
};

/**
Expand Down

0 comments on commit 2453746

Please sign in to comment.