🐛 | Fix prefetch bug (str vs int), move json import to module level, add missing type hints
Some checks failed
SonarQube Code Quality Scan / SonarQube Scan (push) Failing after 1m20s

This commit is contained in:
2026-06-04 16:22:29 +03:00
parent d8ebd6186c
commit 7e9f78ce88
3 changed files with 10 additions and 4 deletions

View File

@@ -414,6 +414,8 @@ class AudioPlayer(QObject):
self._active_task_id: Optional[int] = None
# The task ID of the in-progress prefetch (if any).
self._prefetch_task_id: Optional[int] = None
# The video_id being prefetched (used to avoid duplicate prefetches).
self._prefetch_video_id: Optional[str] = None
# Cache of prefetched results: video_id → resolved dict with _local_path.
self._prefetch_cache: dict[str, dict] = {}
@@ -616,6 +618,7 @@ class AudioPlayer(QObject):
self._worker.cancel_requested.emit()
self._active_task_id = None
self._prefetch_task_id = None
self._prefetch_video_id = None
@Slot(int, dict)
def _on_download_succeeded(self, task_id: int, song: dict) -> None:
@@ -647,6 +650,7 @@ class AudioPlayer(QObject):
# ── Stale check for prefetch download ────────────────────────
if task_id == self._prefetch_task_id:
self._prefetch_task_id = None
self._prefetch_video_id = None
vid = song.get("videoId") or song.get("video_id", "")
# Only cache if the song is still in the active queue.
@@ -680,6 +684,7 @@ class AudioPlayer(QObject):
self._try_next_song()
elif task_id == self._prefetch_task_id:
self._prefetch_task_id = None
self._prefetch_video_id = None
log.debug("Prefetch failed for task %d (harmless)", task_id)
else:
log.debug("Ignoring stale download failure for task %d", task_id)
@@ -818,7 +823,7 @@ class AudioPlayer(QObject):
# Already cached or already downloading.
if vid in self._prefetch_cache:
return
if vid == self._prefetch_task_id:
if vid == self._prefetch_video_id:
return
# Don't prefetch if it's already the active download target.
if self._active_task_id is not None:
@@ -831,6 +836,7 @@ class AudioPlayer(QObject):
task_id = self._next_task_id
self._next_task_id += 1
self._prefetch_task_id = task_id
self._prefetch_video_id = vid
self._worker.download_requested.emit(task_id, next_song)