Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NODE-4429): select server sync for endSessions during close #3363

Merged
merged 4 commits into from Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 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,16 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {

Promise.all(activeSessionEnds)
.then(() => {
// 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;
dariakp marked this conversation as resolved.
Show resolved Hide resolved
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 +522,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', () => {
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
const serverDescription = new ServerDescription('a:1');

it('short circuits and does not end sessions', async () => {
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
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);
});
});
});
});