From 9855d4690146e0d535f3a2d7e4d6ca69f2ca7061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0ar=C5=ABnas=20Nejus?= Date: Fri, 6 Mar 2026 20:51:18 +0000 Subject: [PATCH] Bound Spotify auth retry during search Replace recursive 401 handling with a single retry loop. This prevents unbounded recursion when authentication keeps failing. --- beetsplug/spotify.py | 45 +++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/beetsplug/spotify.py b/beetsplug/spotify.py index 0086f632f..53eae5785 100644 --- a/beetsplug/spotify.py +++ b/beetsplug/spotify.py @@ -27,6 +27,7 @@ import re import threading import time import webbrowser +from http import HTTPStatus from typing import TYPE_CHECKING, Any, ClassVar, Literal import confuse @@ -477,27 +478,33 @@ class SpotifyPlugin( :param filters: Field filters to apply. :param query_string: Additional query to include in the search. """ - response = requests.get( - self.search_url, - headers={"Authorization": f"Bearer {self.access_token}"}, - params={ - **params.filters, - "q": params.query, - "type": params.query_type, - "limit": str(params.limit), - }, - timeout=10, - ) - try: - response.raise_for_status() - except requests.exceptions.HTTPError: - if response.status_code == 401: - self._authenticate() - return self.get_search_response(params) + for _ in range(2): + response = requests.get( + self.search_url, + headers={"Authorization": f"Bearer {self.access_token}"}, + params={ + **params.filters, + "q": params.query, + "type": params.query_type, + "limit": str(params.limit), + }, + timeout=10, + ) + try: + response.raise_for_status() + except requests.exceptions.HTTPError: + if response.status_code == HTTPStatus.UNAUTHORIZED: + self._authenticate() + continue + raise - raise + return ( + response.json() + .get(f"{params.query_type}s", {}) + .get("items", []) + ) - return response.json().get(f"{params.query_type}s", {}).get("items", []) + return () def commands(self) -> list[ui.Subcommand]: # autotagger import command