Skip to content

Commit b365c50

Browse files
authored
fix: awaitable isMaster timeout must respect connectTimeoutMS (#2627)
NODE-2874
1 parent a827807 commit b365c50

File tree

2 files changed

+71
-10
lines changed

2 files changed

+71
-10
lines changed

lib/core/sdam/monitor.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -200,16 +200,19 @@ function checkServer(monitor, callback) {
200200
const topologyVersion = monitor[kServer].description.topologyVersion;
201201
const isAwaitable = topologyVersion != null;
202202

203-
const cmd = isAwaitable
204-
? { ismaster: true, maxAwaitTimeMS, topologyVersion: makeTopologyVersion(topologyVersion) }
205-
: { ismaster: true };
206-
207-
const options = isAwaitable
208-
? { socketTimeout: connectTimeoutMS + maxAwaitTimeMS, exhaustAllowed: true }
209-
: { socketTimeout: connectTimeoutMS };
210-
211-
if (isAwaitable && monitor[kRTTPinger] == null) {
212-
monitor[kRTTPinger] = new RTTPinger(monitor[kCancellationToken], monitor.connectOptions);
203+
const cmd = { ismaster: true };
204+
const options = { socketTimeout: connectTimeoutMS };
205+
206+
if (isAwaitable) {
207+
cmd.maxAwaitTimeMS = maxAwaitTimeMS;
208+
cmd.topologyVersion = makeTopologyVersion(topologyVersion);
209+
if (connectTimeoutMS) {
210+
options.socketTimeout = connectTimeoutMS + maxAwaitTimeMS;
211+
}
212+
options.exhaustAllowed = true;
213+
if (monitor[kRTTPinger] == null) {
214+
monitor[kRTTPinger] = new RTTPinger(monitor[kCancellationToken], monitor.connectOptions);
215+
}
213216
}
214217

215218
monitor[kConnection].command('admin.$cmd', cmd, options, (err, result) => {

test/functional/mongo_client_options.test.js

+58
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
const test = require('./shared').assert;
33
const setupDatabase = require('./shared').setupDatabase;
44
const expect = require('chai').expect;
5+
const sinon = require('sinon');
6+
const Connection = require('../../lib/cmap/connection').Connection;
57

68
describe('MongoClient Options', function() {
79
before(function() {
@@ -110,6 +112,62 @@ describe('MongoClient Options', function() {
110112
});
111113
});
112114

115+
it('must respect an infinite connectTimeoutMS for the streaming protocol', {
116+
metadata: { requires: { topology: 'replicaset', mongodb: '>= 4.4' } },
117+
test: function(done) {
118+
if (!this.configuration.usingUnifiedTopology()) return done();
119+
const client = this.configuration.newClient({
120+
connectTimeoutMS: 0,
121+
heartbeatFrequencyMS: 500
122+
});
123+
client.connect(err => {
124+
expect(err).to.not.exist;
125+
const stub = sinon.stub(Connection.prototype, 'command').callsFake(function() {
126+
const args = Array.prototype.slice.call(arguments);
127+
const ns = args[0];
128+
const command = args[1];
129+
const options = args[2];
130+
if (ns === 'admin.$cmd' && command.ismaster && options.exhaustAllowed) {
131+
stub.restore();
132+
expect(options)
133+
.property('socketTimeout')
134+
.to.equal(0);
135+
client.close(done);
136+
}
137+
stub.wrappedMethod.apply(this, args);
138+
});
139+
});
140+
}
141+
});
142+
143+
it('must respect a finite connectTimeoutMS for the streaming protocol', {
144+
metadata: { requires: { topology: 'replicaset', mongodb: '>= 4.4' } },
145+
test: function(done) {
146+
if (!this.configuration.usingUnifiedTopology()) return done();
147+
const client = this.configuration.newClient({
148+
connectTimeoutMS: 10,
149+
heartbeatFrequencyMS: 500
150+
});
151+
client.connect(err => {
152+
expect(err).to.not.exist;
153+
const stub = sinon.stub(Connection.prototype, 'command').callsFake(function() {
154+
const args = Array.prototype.slice.call(arguments);
155+
const ns = args[0];
156+
const command = args[1];
157+
const options = args[2];
158+
if (ns === 'admin.$cmd' && command.ismaster && options.exhaustAllowed) {
159+
stub.restore();
160+
expect(options)
161+
.property('socketTimeout')
162+
.to.equal(510);
163+
client.close(done);
164+
}
165+
stub.wrappedMethod.apply(this, args);
166+
});
167+
});
168+
}
169+
});
170+
113171
/**
114172
* @ignore
115173
*/

0 commit comments

Comments
 (0)