Skip to content

fix(NODE-5155) release connection lock when topology closed #3618

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/mongo_client.ts
Original file line number Diff line number Diff line change
@@ -450,6 +450,9 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
topology.client = this;

topology.once(Topology.OPEN, () => this.emit('open', this));
topology.on(Topology.TOPOLOGY_CLOSED, () => {
this.connectionLock = undefined;
});

for (const event of MONGO_CLIENT_EVENTS) {
topology.on(event, (...args: any[]) => this.emit(event, ...(args as any)));
10 changes: 10 additions & 0 deletions test/integration/node-specific/mongo_client.test.ts
Original file line number Diff line number Diff line change
@@ -574,6 +574,16 @@ describe('class MongoClient', function () {
expect(topologyOpenEvents).to.have.lengthOf(1);
expect(client.topology?.isConnected()).to.be.true;
});

it('releases the lock if the topology is closed before connection is complete', function (done) {
clientConnect();
client.close();
client.once('topologyClosed', async () => {
await clientConnect();
expect(client.topology?.isConnected()).to.be.true;
done();
});
});
});

context('#close()', () => {