Skip to content

Commit 68da446

Browse files
src code changes
1 parent 41b066b commit 68da446

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/mongo_client.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,8 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
366366
override readonly mongoLogger: MongoLogger | undefined;
367367
/** @internal */
368368
private connectionLock?: Promise<this>;
369+
/** @internal */
370+
private closeLock?: Promise<void>
369371

370372
/**
371373
* The consolidate, parsed, transformed and merged options.
@@ -638,6 +640,20 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
638640
* @param force - Force close, emitting no events
639641
*/
640642
async close(force = false): Promise<void> {
643+
if (this.closeLock) {
644+
return await this.closeLock;
645+
}
646+
647+
try {
648+
this.closeLock = this._close(force);
649+
await this.closeLock;
650+
} finally {
651+
// release
652+
this.connectionLock = undefined;
653+
}
654+
}
655+
656+
async _close(force = false): Promise<void> {
641657
// There's no way to set hasBeenClosed back to false
642658
Object.defineProperty(this.s, 'hasBeenClosed', {
643659
value: true,

0 commit comments

Comments
 (0)