Repository navigation
Pause all accounts on a Spotify rate limit instead of sleeping in the executor - #69
Merged
Merged
Conversation
… executor spotipy retried a 429 by sleeping the full Retry-After (capped at 6 h by urllib3) inside the Home Assistant executor thread, up to 3 times, once per account and per action call. A rate limit therefore left coordinator refreshes and play_media calls hanging for hours, and every waking thread immediately re-hit an API that had already said no. - Remove 429 from spotipy's retry codes (5xx retries unchanged). - Add a shared RateLimitGuard consulted by every Spotify client before a request: a 429 registers Retry-After (default 30 s when missing) and raises RateLimitedError; every account fails fast with no network call until the window expires, then resumes on its own. - RateLimitedError subclasses SpotifyException (429) so existing handlers keep working; the coordinator and service handler report the resume time, playback wrappers let it through, system health shows a Rate Limit row. Fixes #68 Co-Authored-By: Claude Fable 5 <[email protected]>
urllib3 retries any 429/503 carrying a Retry-After header whenever respect_retry_after_header is set, regardless of status_forcelist, and spotipy's session builder gives no way to turn it off. Verified through the real requests/urllib3 stack against a local server answering like Spotify: a 429 now returns immediately with a single request, 5xx keep their 3 retries. Adds real-stack tests for both. Co-Authored-By: Claude Fable 5 <[email protected]>
Owner
Author
Simulation of the reporter's condition (real HTTP stack)Local server impersonating The simulation caught a real gap in the first commit: removing 429 from |
Merged
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.
Fixes #68
Problem
Spotify()was built with spotipy's defaults, which retry a429through urllib3'sRetrywithrespect_retry_after_header=True. That meanstime.sleep(Retry-After)(capped at 6 h by urllib3) inside the Home Assistant executor thread, up to 3 times, for every call that gets a 429. With the reporter'sRetry-Aftervalues (2,484 s to 12,471 s) a single coordinator refresh orplay_mediacall could hang for hours, once per account, and nothing in spotcast readRetry-Afteror told the other accounts (same client id, same rate limit) to stop.Fix
spotify/client.py: remove 429 from spotipy'sstatus_forcelist(5xx retries unchanged, the fake-429-on-5xx unmasking moved here asSERVER_ERROR_PATTERN/is_rate_limit()), and consult a sharedRateLimitGuardbefore every request. A genuine 429 registersRetry-After(default 30 s when missing, never shortens an existing window) and raisesRateLimitedError; until the window expires every client fails fast with no network call, then resumes on its own.spotify/rate_limit.py(new): thread-safe guard, one WARNING per window then DEBUG.spotify/exceptions.py:RateLimitedError(SpotifyException)with status 429,retry_at,seconds_remaining,resume_time, so existingexcept SpotifyExceptionpaths keep working.coordinator.py: "Spotify rate limit active for this client id. Refresh of entry...paused for N s (until HH:MM:SS)".services/service_handler.py:HomeAssistantError("Spotify is rate limiting this client id. Try again in N s (after HH:MM:SS)")instead of a traceback.spotify/account/playback.py:RateLimitedErrorpasses through thePlaybackErrorwrappers (device-not-found retry extracted into_async_retry_when_device_appears).system_health.py:Rate Limitrow (ok/limited until HH:MM:SS).Tests
35 new tests (guard, client, coordinator, service handler, playback, system health).
task test: 791 passed.pylint custom_components: 10.00/10.🤖 Generated with Claude Code