Repository navigation
fix: 401 "Access token missing" error screen at every song transition (#395) - #399
Merged
Merged
Conversation
…#395) Spotify's player service intermittently rejects a valid token on me/player, typically right at a track transition. The app turned that blip into a visible failure three ways, all fixed here: - Every 401 forced a full token refresh, which under PKCE also rotates the refresh token. At the 1s external-device poll cadence that minted a new token family every few seconds. Forced refreshes are now rate limited to one per 30s by a shared ForcedRefreshGate, and a 401 inside the cooldown retries with the token already in hand. - The single retry fired immediately, landing back inside the window that produced the 401. It now backs off 1s first. Recovery stays one-shot on both paths. - The playback poll treated the surfaced 401 as fatal. It now tolerates a short run (silent on the first, status message from the second, error route on the fourth) and resets on every poll the API answers. Also adds diagnostics on every non-2xx response (endpoint, status, body, and the age of the attached token, never the token itself), and reports which Spotify app the session signed in as: the setup wizard writes the shared ncspot client ID as the primary for both of its options and keeps your own app as the fallback, so "I set up my own app" never meant "I am using my own app". The shared request helper's parameters were grouped into a SpotifyApiRequest struct, since the extra gate argument tripped clippy::too_many_arguments.
Contributor
📝 WalkthroughWalkthroughThe PR adds Spotify client ID authentication notices, throttles forced token refreshes after repeated 401 responses, tolerates transient playback polling failures, improves response diagnostics, and resets refresh state after login. ChangesSpotify authentication and playback handling
Estimated code review effort: 4 (Complex) | ~45 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
✨ Simplify code
Comment |
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
Fixes #395.
With playback on an external Connect device, spotatui showed the full-screen error route at every track transition with a 401
Access token missingfromme/player, even though the music never stopped and the very next poll succeeded with the same token.The 401 itself comes from Spotify's player-service gateway (only
me/player*ever produces that message, and no client-side mistake reproduces it), but the app amplified a transient blip into a visible failure in three ways, all fixed here:POST /api/tokenevery few seconds visible in the reporter logs, and a lost rotated refresh token logs the user out. Forced refreshes are now rate limited to one per 30 seconds by a sharedForcedRefreshGate; a 401 inside the cooldown retries with the token already in hand.Two supporting changes:
client_idfor both of its options and keeps your own app asfallback_client_id, so "I set up my own app" has never meant "I am using my own app". Startup now says so. This cost real diagnostic time on 401 "Access token missing" shown after every song transition while playback continues in the background #395: the reporter believed they were on their own client ID and were not.The shared request helper's parameters were grouped into a
SpotifyApiRequeststruct, since the extra gate argument trippedclippy::too_many_arguments.Testing
cargo fmt --allcargo clippy --no-default-features --features telemetry -- -D warnings, cleancargo test --no-default-features --features telemetry, 483 passedcargo test, 731 passedcargo test --features all-sources, 804 passedNew tests:
second_unauthorized_within_cooldown_retries_without_forcing_refresh: two calls, each answered 401 then 200, asserting exactly one forced refresh across both and that the second call's retry reuses the already-refreshed token.unauthorized_forces_refresh_again_once_cooldown_expires: an elapsed cooldown still recovers a token that really did go bad.repeated_unauthorized_in_one_call_gives_up_after_one_recovery: termination, plus the status stays in the error text thatplayback.rsmatches on.retries_once_with_refreshed_token_after_401extended to assert the retry is backed off.client_id_notice_forcovering wizard option 1, wizard option 2, a hand-edited personal-primary config, and a genuine fallback.Not covered by tests: the poll escalation threshold against a real external Connect device. That needs a manual pass, which has not been done yet.
Additional notes
IoEvent::RefreshAuthenticationon every poll 401, which runs a forcedrefresh_token_and_cache, so at the 1 second cadence it adds a PKCE refresh-token rotation per second on top of the one the request path already performs. It also has no escalation path, so a genuinely dead session shows "session expired, refreshing automatically" forever. The two changes conflict textually in the sameErrarm.Summary by CodeRabbit
Bug Fixes
User Experience