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-5225): eagerly clear MongoClient.topology in MongoClient.close() #3652

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions src/mongo_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,10 +504,14 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
return;
}

// clear out references to old topology
const topology = this.topology;
this.topology = undefined;

// 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 topologyDescription = topology.description;
const serverDescriptions = Array.from(topologyDescription.servers.values());
const servers = selector(topologyDescription, serverDescriptions);
if (servers.length !== 0) {
Expand All @@ -522,10 +526,6 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
}
}

// clear out references to old topology
const topology = this.topology;
this.topology = undefined;

await new Promise<void>((resolve, reject) => {
topology.close({ force }, error => {
if (error) return reject(error);
Expand Down
8 changes: 8 additions & 0 deletions test/integration/node-specific/mongo_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,14 @@ describe('class MongoClient', function () {
}
});

it('can call close() concurrently', async function () {
const client = this.configuration.newClient();
await client.connect();
// Ensure topology is opened before trying to close
await client.db().command({ hello: 1 });
await Promise.all([client.close(), client.close()]);
});

context('explict #connect()', () => {
let client: MongoClient;
beforeEach(function () {
Expand Down