fix(audio): silently retry on 401 from /api/calls/:id/audio#30
Merged
Conversation
When a sibling device (phone, tablet, second tab) logs in and pushes the desktop's access JWT out of the per-user 5-token concurrent cap, the desktop's session cookie now carries a revoked JWT. The WS connection survives because it auths once at connect time, but every subsequent <audio> fetch returns 401 — and unlike RTK Query traffic those don't go through the auto-refresh path. Add a recovery hook on the audio player: when the element fires 'error', POST /api/auth/refresh once. The fresh Set-Cookie installs a new os_session, and we retry the same call. Subsequent failures on the same item give up and skip to the next, so we can't loop.
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.
Symptom
After logging in on a phone with the same account, desktop playback starts failing with:
The WS keeps delivering CALs (so the live feed UI looks fine), but every
<audio src=…>fetch returns 401.Root cause
The backend caps each user at 5 concurrent access JWTs (
auth.MaxRefreshFamilies = 5,TokenTracker.MaxTokens = 5). Every login and every silent refresh issues a new JWT and bumps the oldest off the active list. Desktop + phone with 15-minute access TTL and 1-minute-pre-expiry refresh blows through 5 slots in roughly an hour, so the desktop's JWT \u2014 and therefore itsos_sessioncookie \u2014 ends up server-side-revoked.The WS connection survives because it authenticates once at connect time and isn't re-checked. But every
<audio src=…>request re-authenticates from scratch. Unlike RTK Query traffic, media-element fetches don't go throughbaseQueryWithRefresh, so a 401 there isn't auto-recovered.Fix
Add an auth-recovery hook on the audio player. When the element fires
errorfor the current item, the player invokes the recovery callback once before skipping.main.tsxwires the callback to:POST /api/auth/refreshwithcredentials: include. Server installs a freshos_sessioncookie and returns a fresh access JWT.store.dispatch(setCredentials(\u2026))so the in-memory bearer is in sync (WS reconnects pick it up).audio.load()to re-fetch the same URL with the new cookie, thenplay().If the refresh fails, or playback errors again on the retry, the player gives up and advances the queue. The per-item
recoveryTriedflag prevents loops.Verification
pnpm exec tsc --noEmitclean.Self-review
fetchwithcredentials: includeagainst the same origin; no token leaves Redux memory; the cookie remains httpOnly.publicAccess=truedeployments continue to work because the recovery callback's failure is silent and doesn't gate playback startup \u2014 unauthenticated 401s still skip the same as before.Note for follow-up
Consider raising
auth.MaxRefreshFamilies/TokenTracker.MaxTokensfrom 5 to something more accommodating (10\u201320). With access TTL=15min and refresh-1-min-before, two devices generate ~10 tokens/hour, which is borderline. Out of scope for this PR.