Fix: delete stale Download objects when clearing cache#663
Open
GeiserX wants to merge 2 commits into
Open
Conversation
When "Delete downloaded songs and podcast episodes" clears the cache, `deleteCacheFinalStep` sets `relFilePath` to nil but leaves the associated `DownloadMO` relationship intact. `getSongsForCompleteLibraryDownload` requires BOTH `relFilePath == nil` AND `download == nil` to consider a song eligible for download. The stale `DownloadMO` causes every song to be filtered out, making "Download all songs in library" silently do nothing after a cache clear. This follows the same pattern used when deleting songs (lines 185-186) and podcast episodes (lines 217-218), which already delete the associated `DownloadMO` via `context.delete(download)`. Fixes BLeeEZ#662
The previous commit prevents new stale DownloadMOs from being created during cache clear. However, users upgrading from older builds may already have stale DownloadMOs (download != nil but relFilePath == nil), which still block getSongsForCompleteLibraryDownload. Remove the download == nil predicate from the fetch. This is safe because DownloadRequestManager.addLowPrio already handles existing downloads correctly: if the song is not cached (!object.isCached), it resets the download and re-queues it (line 106). Songs that are already cached are skipped (line 111). So the predicate was redundant with addLowPrio's own deduplication logic. This makes the fix both preventive (commit 1) and migratory (this commit) — existing broken accounts self-heal on next "Download all songs in library" without requiring manual cleanup.
Author
Validation ResultsTested both commits on macOS (patched build from this PR branch) against Navidrome with ALAC library (~1636 songs). Repro matrix
Key observations
Navidrome log excerpt (hostname account, post-fix) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Root Cause
Two issues combine to produce the bug:
1. Stale DownloadMO after cache clear (preventive fix — commit 1)
deleteCacheFinalStep(called by "Delete downloaded songs and podcast episodes") setsrelFilePath = nilbut leaves the associatedDownloadMOCore Data relationship intact. This follows the same pattern already used when resolving duplicate songs (line 185-186) and podcast episodes (line 217-218), which already delete the associatedDownloadMOviacontext.delete(download).2. Overly restrictive fetch predicate (self-healing fix — commit 2)
getSongsForCompleteLibraryDownloadrequires both predicates to match:The
download == nilpredicate is redundant withDownloadRequestManager.addLowPrio, which already correctly handles existing downloads: if the song is not cached (!object.isCached), it resets the download and re-queues it. Songs that are already cached are skipped. Removing this predicate allowsaddLowPrio's own deduplication logic to handle both fresh and stale state.Why both commits
DownloadMOduring cache clear so the stale state is never created going forward.download == nilpredicate so users upgrading from older builds self-heal on next "Download all" without manual cleanup.Test Plan