fix(07): CR-03 close IMAP connection when getMailboxLock throws
Tessera CI/CD / Lint & Type Check (push) Waiting to run
Tessera CI/CD / Tests (push) Blocked by required conditions
Tessera CI/CD / Build & Deploy (push) Blocked by required conditions

If getMailboxLock() failed (e.g. folder not found) the try/finally cleanup
block was never entered, leaving the ImapFlow connection open and leaking.
Move the lock acquisition inside the try block and use lock?.release() in
finally so client.logout() is always called regardless of lock success.
This commit is contained in:
2026-06-27 17:20:39 +02:00
parent 955a94638c
commit cb0d378ded
+6 -2
View File
@@ -104,10 +104,14 @@ export class ImapProvider implements InboxProvider {
const client = this.buildClient(config); const client = this.buildClient(config);
await client.connect(); await client.connect();
const lock = await client.getMailboxLock(config.folder || 'INBOX'); // lock is declared outside try so the type is available in finally, but
// getMailboxLock() itself is inside the try so that a lock failure still
// triggers client.logout() — preventing a connection leak (CR-03).
let lock: Awaited<ReturnType<typeof client.getMailboxLock>> | null = null;
const results: InboxEmail[] = []; const results: InboxEmail[] = [];
try { try {
lock = await client.getMailboxLock(config.folder || 'INBOX');
// Search for emails from the configured sender // Search for emails from the configured sender
const searchQuery = config.senderFilter const searchQuery = config.senderFilter
? { from: config.senderFilter } ? { from: config.senderFilter }
@@ -170,7 +174,7 @@ export class ImapProvider implements InboxProvider {
} }
} }
} finally { } finally {
lock.release(); lock?.release();
await client.logout(); await client.logout();
} }