Skip to content

fix: transition topology state before async calls #2637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions lib/core/topologies/replset.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ var monitorServer = function(host, self, options) {
self.s.options.secondaryOnlyConnectionAllowed) ||
self.s.replicaSetState.hasPrimary())
) {
self.state = CONNECTED;
stateTransition(self, CONNECTED);

// Emit connected sign
process.nextTick(function() {
Expand All @@ -558,7 +558,7 @@ var monitorServer = function(host, self, options) {
self.s.options.secondaryOnlyConnectionAllowed) ||
self.s.replicaSetState.hasPrimary())
) {
self.state = CONNECTED;
stateTransition(self, CONNECTED);

// Rexecute any stalled operation
rexecuteOperations(self);
Expand Down Expand Up @@ -787,7 +787,7 @@ function handleInitialConnectEvent(self, event) {
// Do we have a primary or primaryAndSecondary
if (shouldTriggerConnect(self)) {
// We are connected
self.state = CONNECTED;
stateTransition(self, CONNECTED);

// Set initial connect state
self.initialConnectState.connect = true;
Expand Down Expand Up @@ -975,14 +975,19 @@ ReplSet.prototype.destroy = function(options, callback) {
// Emit toplogy closing event
emitSDAMEvent(this, 'topologyClosed', { topologyId: this.id });

// Transition state
stateTransition(this, DESTROYED);

if (typeof callback === 'function') {
callback(null, null);
}
};

if (this.state === DESTROYED) {
if (typeof callback === 'function') callback(null, null);
return;
}

// Transition state
stateTransition(this, DESTROYED);

// Clear out any monitoring process
if (this.haTimeoutId) clearTimeout(this.haTimeoutId);

Expand Down
5 changes: 3 additions & 2 deletions lib/core/topologies/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -864,12 +864,14 @@ Server.prototype.destroy = function(options, callback) {
}

// No pool, return
if (!self.s.pool) {
if (!self.s.pool || this._destroyed) {
this._destroyed = true;
if (typeof callback === 'function') callback(null, null);
return;
}

this._destroyed = true;

// Emit close event
if (options.emitClose) {
self.emit('close', self);
Expand Down Expand Up @@ -900,7 +902,6 @@ Server.prototype.destroy = function(options, callback) {

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

/**
Expand Down
36 changes: 12 additions & 24 deletions test/functional/core/rs_mocks/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ describe('ReplSet Connection Tests (mocks)', function() {
expect(server.s.replicaSetState.primary).to.not.be.null;
expect(server.s.replicaSetState.primary.name).to.equal('localhost:32000');

server.destroy();
done();
server.destroy(done);
}
}
});
Expand Down Expand Up @@ -251,8 +250,7 @@ describe('ReplSet Connection Tests (mocks)', function() {
expect(server.s.replicaSetState.primary).to.not.be.null;
expect(server.s.replicaSetState.primary.name).to.equal('localhost:32000');

server.destroy();
done();
server.destroy(done);
}
}
});
Expand Down Expand Up @@ -351,8 +349,7 @@ describe('ReplSet Connection Tests (mocks)', function() {
expect(server.s.replicaSetState.primary).to.not.be.null;
expect(server.s.replicaSetState.primary.name).to.equal('localhost:32000');

server.destroy();
done();
server.destroy(done);
}

// Joined
Expand Down Expand Up @@ -428,8 +425,7 @@ describe('ReplSet Connection Tests (mocks)', function() {
);

server.on('error', function() {
server.destroy();
done();
server.destroy(done);
});

server.connect();
Expand Down Expand Up @@ -528,8 +524,7 @@ describe('ReplSet Connection Tests (mocks)', function() {

expect(server.s.replicaSetState.primary).to.be.null;

server.destroy();
done();
server.destroy(done);
}
});

Expand Down Expand Up @@ -652,8 +647,7 @@ describe('ReplSet Connection Tests (mocks)', function() {
expect(server.s.replicaSetState.primary).to.not.be.null;
expect(server.s.replicaSetState.primary.name).to.equal('localhost:32000');

server.destroy();
done();
server.destroy(done);
}
}
});
Expand Down Expand Up @@ -759,8 +753,7 @@ describe('ReplSet Connection Tests (mocks)', function() {
);

server.on('error', function() {
server.destroy();
done();
server.destroy(done);
});

// Gives proxies a chance to boot up
Expand Down Expand Up @@ -857,8 +850,7 @@ describe('ReplSet Connection Tests (mocks)', function() {
expect(server.s.replicaSetState.primary).to.not.be.null;
expect(server.s.replicaSetState.primary.name).to.equal('localhost:32000');

server.destroy();
done();
server.destroy(done);
}
}
});
Expand Down Expand Up @@ -979,8 +971,7 @@ describe('ReplSet Connection Tests (mocks)', function() {
expect(server.s.replicaSetState.primary).to.not.be.null;
expect(server.s.replicaSetState.primary.name).to.equal('localhost:32000');

server.destroy();
done();
server.destroy(done);
}
}
});
Expand Down Expand Up @@ -1070,8 +1061,7 @@ describe('ReplSet Connection Tests (mocks)', function() {
expect(server.s.replicaSetState.primary).to.not.be.null;
expect(server.s.replicaSetState.primary.name).to.equal('localhost:32000');

server.destroy();
done();
server.destroy(done);
}
}
});
Expand Down Expand Up @@ -1182,8 +1172,7 @@ describe('ReplSet Connection Tests (mocks)', function() {
expect(server.__connected).to.be.true;
expect(server.__fullsetup).to.be.true;

server.destroy();
done();
server.destroy(done);
});

server.connect();
Expand Down Expand Up @@ -1274,8 +1263,7 @@ describe('ReplSet Connection Tests (mocks)', function() {
var result = server.lastIsMaster();
expect(result).to.exist;

server.destroy();
done();
server.destroy(done);
});

server.connect();
Expand Down