Fix: Use yt_dlp.utils.DownloadCancelled for proper download cancellation

- Import and use DownloadCancelled exception which YT-DLP properly handles
- Add InterruptedError handling throughout the call chain
- Fire 'cancelled' status event when download is cancelled
- Handle InterruptedError in DownloadService to set CANCELLED status
This commit is contained in:
2025-12-27 19:38:12 +01:00
parent 08f816a954
commit 4780f68a23
4 changed files with 74 additions and 9 deletions

View File

@@ -952,7 +952,7 @@ class DownloadService:
except asyncio.CancelledError:
# Handle task cancellation during shutdown
logger.info(
"Download cancelled during shutdown: item_id=%s",
"Download task cancelled: item_id=%s",
item.id,
)
item.status = DownloadStatus.CANCELLED
@@ -965,6 +965,23 @@ class DownloadService:
# Re-save to database as pending
await self._save_to_database(item)
raise # Re-raise to properly cancel the task
except InterruptedError:
# Handle download cancellation from provider
logger.info(
"Download interrupted/cancelled: item_id=%s",
item.id,
)
item.status = DownloadStatus.CANCELLED
item.completed_at = datetime.now(timezone.utc)
# Delete cancelled item from database
await self._delete_from_database(item.id)
# Return item to pending queue if not shutting down
if not self._is_shutting_down:
self._add_to_pending_queue(item, front=True)
# Re-save to database as pending
await self._save_to_database(item)
# Don't re-raise - this is handled gracefully
except Exception as e:
# Handle failure