Skip to content

Commit

Permalink
fix(NODE-4429): select server sync for endSessions during close (#3363)
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Aug 17, 2022
1 parent f28ba0d commit 5086ead
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/mongo_client.ts
Expand Up @@ -21,6 +21,7 @@ import { PromiseProvider } from './promise_provider';
import type { ReadConcern, ReadConcernLevel, ReadConcernLike } from './read_concern';
import { ReadPreference, ReadPreferenceMode } from './read_preference';
import type { TagSet } from './sdam/server_description';
import { readPreferenceServerSelector } from './sdam/server_selection';
import type { SrvPoller } from './sdam/srv_polling';
import type { Topology, TopologyEvents } from './sdam/topology';
import { ClientSession, ClientSessionOptions, ServerSessionPool } from './sessions';
Expand Down Expand Up @@ -500,6 +501,19 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {

Promise.all(activeSessionEnds)
.then(() => {
if (this.topology == null) {
return;
}
// If we would attempt to select a server and get nothing back we short circuit
// to avoid the server selection timeout.
const selector = readPreferenceServerSelector(ReadPreference.primaryPreferred);
const topologyDescription = this.topology.description;
const serverDescriptions = Array.from(topologyDescription.servers.values());
const servers = selector(topologyDescription, serverDescriptions);
if (servers.length === 0) {
return;
}

const endSessions = Array.from(this.s.sessionPool.sessions, ({ id }) => id);
if (endSessions.length === 0) return;
return this.db('admin')
Expand All @@ -511,7 +525,7 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
})
.then(() => {
if (this.topology == null) {
return callback();
return;
}
// clear out references to old topology
const topology = this.topology;
Expand Down
22 changes: 22 additions & 0 deletions test/integration/node-specific/mongo_client.test.ts
Expand Up @@ -13,6 +13,7 @@ import {
} from '../../../src';
import { Connection } from '../../../src/cmap/connection';
import { Db } from '../../../src/db';
import { ServerDescription } from '../../../src/sdam/server_description';
import { Topology } from '../../../src/sdam/topology';
import { getTopology, isHello } from '../../../src/utils';
import { runLater } from '../../tools/utils';
Expand Down Expand Up @@ -649,5 +650,26 @@ describe('class MongoClient', function () {
expect(endEvents).to.have.lengthOf(1);
expect(endEvents[0]).to.have.property('reply', undefined); // noReponse: true
});

context('when server selection would return no servers', () => {
const serverDescription = new ServerDescription('a:1');

it('short circuits and does not end sessions', async () => {
const session = client.startSession(); // make a session to be ended
await client.db('test').command({ ping: 1 }, { session });
await session.endSession();

const startedEvents: CommandStartedEvent[] = [];
client.on('commandStarted', event => startedEvents.push(event));

const servers = new Map<string, ServerDescription>();
servers.set(serverDescription.address, serverDescription);
client.topology.description.servers = servers;
await client.close();

expect(startedEvents).to.be.empty;
expect(client.s.sessionPool.sessions).to.have.lengthOf(1);
});
});
});
});

0 comments on commit 5086ead

Please sign in to comment.