fix stale IMAP connection: clean up old connection on reconnect, robust disconnect

- connect() now cleans up any stale connection/event loop group before
  creating a new one, preventing leaked resources after failed syncs
- disconnect() uses try? for both channel close and group shutdown so
  "Already closed" errors don't prevent cleanup

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-15 12:10:53 +01:00
parent 01605a01ec
commit df78397870

View File

@@ -20,6 +20,14 @@ public actor IMAPClient: IMAPClientProtocol {
// MARK: - Connection lifecycle
public func connect() async throws {
// Clean up any stale connection from a previous failed sync
if connection != nil {
try? await connection?.disconnect()
try? await connection?.shutdown()
connection = nil
runner = nil
}
let conn = IMAPConnection(host: host, port: port)
try await conn.connect()
connection = conn
@@ -36,8 +44,8 @@ public actor IMAPClient: IMAPClientProtocol {
_ = try? await r.run(.logout)
runner = r
}
try await connection?.disconnect()
try await connection?.shutdown()
try? await connection?.disconnect()
try? await connection?.shutdown()
connection = nil
runner = nil
}