From 577282cf078f3dc7b4c6061c3031a9a251d6a4e9 Mon Sep 17 00:00:00 2001 From: Alec Gibson <12036746+alecgibson@users.noreply.github.com> Date: Fri, 31 Mar 2023 10:43:13 +0100 Subject: [PATCH] fix(NODE-5155) release connection lock when topology closed --- src/mongo_client.ts | 3 +++ test/integration/node-specific/mongo_client.test.ts | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/mongo_client.ts b/src/mongo_client.ts index 7e73b4c9b5..ffd0ab06e1 100644 --- a/src/mongo_client.ts +++ b/src/mongo_client.ts @@ -450,6 +450,9 @@ export class MongoClient extends TypedEventEmitter { 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))); diff --git a/test/integration/node-specific/mongo_client.test.ts b/test/integration/node-specific/mongo_client.test.ts index f92ccee05a..6ba88f9b9f 100644 --- a/test/integration/node-specific/mongo_client.test.ts +++ b/test/integration/node-specific/mongo_client.test.ts @@ -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()', () => {