Skip to content

Commit 1d02768

Browse files
fix connection leak
1 parent 6247039 commit 1d02768

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

src/sdam/server.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,29 @@ export class Server extends TypedEventEmitter<ServerEvents> {
303303
}
304304
}
305305

306-
const cmd = operation.buildCommand(conn, session);
306+
const cleanup = () => {
307+
this.decrementOperationCount();
308+
if (session?.pinnedConnection !== conn) {
309+
if (reauthPromise != null) {
310+
// The reauth promise only exists if it hasn't thrown.
311+
const checkBackIn = () => {
312+
this.pool.checkIn(conn);
313+
};
314+
void reauthPromise.then(checkBackIn, checkBackIn);
315+
} else {
316+
this.pool.checkIn(conn);
317+
}
318+
}
319+
};
320+
321+
let cmd;
322+
try {
323+
cmd = operation.buildCommand(conn, session);
324+
} catch (e) {
325+
cleanup();
326+
throw e;
327+
}
328+
307329
const options = operation.buildOptions(timeoutContext);
308330
const ns = operation.ns;
309331

@@ -360,18 +382,7 @@ export class Server extends TypedEventEmitter<ServerEvents> {
360382
throw operationError;
361383
}
362384
} finally {
363-
this.decrementOperationCount();
364-
if (session?.pinnedConnection !== conn) {
365-
if (reauthPromise != null) {
366-
// The reauth promise only exists if it hasn't thrown.
367-
const checkBackIn = () => {
368-
this.pool.checkIn(conn);
369-
};
370-
void reauthPromise.then(checkBackIn, checkBackIn);
371-
} else {
372-
this.pool.checkIn(conn);
373-
}
374-
}
385+
cleanup();
375386
}
376387
}
377388

0 commit comments

Comments
 (0)