The ThumbnailManagerRemote was using persistent SSH connection pooling which led to:
- "Not connected - call connect() first" errors
- Stale connection reuse
- Connection state management complexity
- The same safety issues we identified in the main SSH connection analysis
- Refactored
generateFFmpegThumbnail: Now usesSSHConnection.withConnection()pattern - Updated
ensureFFmpegAvailable: Changed parameter name to avoid confusion with old pattern - Added safe helper methods: New static methods for checking and installing ffmpeg safely
- Deprecated connection pooling: Marked old connection pooling as deprecated
- Added safe public method:
ensureFFmpegAvailable(on server:)using create-and-destroy pattern - Kept internal method:
ensureFFmpegAvailable(using connection:)for when connection already exists - Wrapped in withConnection: All SSH operations now use the safe pattern
- No more "Not connected" errors from stale connections
- Fresh connection for each thumbnail generation
- Automatic cleanup on all exit paths
- Connection failures are isolated to individual operations
- No complex connection state management
- Better error recovery
- No shared connection state to coordinate
- Each operation gets its own connection
- Eliminates race conditions
- Uses same safe pattern as other SSH operations
- Matches the refactoring done in SSHConnection and CreateTorrent
// BAD: Uses persistent connection pool
let connection = getConnection(for: server)
let ffmpegPath = await ensureFFmpegAvailable(for: server, connection: connection)
// Connection may be stale or invalid// GOOD: Create-and-destroy pattern
try await SSHConnection.withConnection(server: server) { connection in
try await connection.connect()
let ffmpegPath = await ensureFFmpegAvailable(for: server, using: connection)
// Automatic cleanup guaranteed
}
// OR even simpler with static helpers:
let ffmpegPath = try await ThumbnailManagerRemote.ensureFFmpegInstalled(on: server)- Thumbnail generation is now much more reliable
- FFmpeg installation/detection won't fail due to connection issues
- Consistent with the broader SSH safety improvements
- The old
getConnection()method is still present but marked deprecated - Gradually migrate any remaining uses of the connection pool
- Eventually remove the connection pooling entirely
- Test thumbnail generation after network interruptions
- Test FFmpeg installation on fresh servers
- Test concurrent thumbnail requests to verify no connection conflicts
- Test app backgrounding/foregrounding during thumbnail operations
This refactoring eliminates the root cause of the FFmpeg installation failures and thumbnail generation errors you were experiencing, making the remote thumbnail system much more robust and reliable.