From 5086eadf52e13369f584b9bd64477ab7f3522ff6 Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Wed, 17 Aug 2022 13:33:53 -0400 Subject: [PATCH] fix(NODE-4429): select server sync for endSessions during close (#3363) --- src/mongo_client.ts | 16 +++++++++++++- .../node-specific/mongo_client.test.ts | 22 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/mongo_client.ts b/src/mongo_client.ts index 45f8a240f9..d980f3e792 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,19 @@ export class MongoClient extends TypedEventEmitter { 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') @@ -511,7 +525,7 @@ export class MongoClient extends TypedEventEmitter { }) .then(() => { if (this.topology == null) { - return callback(); + return; } // clear out references to old topology const topology = this.topology; diff --git a/test/integration/node-specific/mongo_client.test.ts b/test/integration/node-specific/mongo_client.test.ts index f459ae3615..e0e005a899 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,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(); + 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); + }); + }); }); });