Fix UnboundLocalError in _check_and_remove_from_wishlist Method 4

When a completed download's track_info has neither an `id` field nor a
`wishlist_id`, Methods 1-3 of _check_and_remove_from_wishlist() all skip
without defining `wishlist_tracks`. Method 4 (fuzzy match) then hits
`if not wishlist_tracks:` and raises UnboundLocalError, which the call
sites catch + log but silently skip the wishlist removal for that track.

Path became more common after the batch-queue-system refactor started
routing non-Spotify-id completions (e.g. discover sync tracks downloaded
under a non-Spotify primary source) through the same completion handler.

Fix: initialize `wishlist_tracks = []` at the top of the try block so
Method 3's reassignment still works and Method 4's `if not wishlist_tracks`
guard always has a defined value to test.

Credit to RENOxDECEPTION (JohnBaumb) for pinpointing the variable-scope
issue during PR #357 testing.
This commit is contained in:
Broque Thomas 2026-04-23 16:52:13 -07:00
parent b4cc3e8975
commit 4b619951ff

View file

@ -22619,7 +22619,11 @@ def _check_and_remove_from_wishlist(context):
# Try to extract Spotify track ID from various sources in the context
spotify_track_id = None
# Populated lazily by Method 3 or Method 4. Initialized here so Method 4's
# `if not wishlist_tracks` guard doesn't UnboundLocalError when Methods 1/2
# found nothing and Method 3 never ran (no wishlist_id in track_info).
wishlist_tracks = []
# Method 1: Direct track_info with id
track_info = context.get('track_info', {})
if track_info.get('id'):