Skip to content

Commit

Permalink
fix(NODE-4516): short circuit end sessions during close
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Aug 16, 2022
1 parent 6425c7a commit ff5cd63
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
12 changes: 12 additions & 0 deletions 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,17 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {

Promise.all(activeSessionEnds)
.then(() => {
// If we would attempt to select a server and get nothing back when using a direct
// connection (which is always topology type single and cannot transition to unknown)
// we short circuit.
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 callback();
}

const endSessions = Array.from(this.s.sessionPool.sessions, ({ id }) => id);
if (endSessions.length === 0) return;
return this.db('admin')
Expand Down
21 changes: 21 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,25 @@ 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;
});
});
});
});

0 comments on commit ff5cd63

Please sign in to comment.