diff --git a/src/mongo_client.ts b/src/mongo_client.ts index 45f8a240f98..5b870fae576 100644 --- a/src/mongo_client.ts +++ b/src/mongo_client.ts @@ -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'; @@ -500,6 +501,17 @@ export class MongoClient extends TypedEventEmitter { 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') diff --git a/test/integration/node-specific/mongo_client.test.ts b/test/integration/node-specific/mongo_client.test.ts index f459ae36152..6081af99c68 100644 --- a/test/integration/node-specific/mongo_client.test.ts +++ b/test/integration/node-specific/mongo_client.test.ts @@ -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'; @@ -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(); + servers.set(serverDescription.address, serverDescription); + client.topology.description.servers = servers; + await client.close(); + + expect(startedEvents).to.be.empty; + }); + }); }); });