Skip to content

fix(auth): refresh the token on INVALID_TOKEN as well as EXPIRED_TOKEN [review/suggestion] - #185

Open
chrishagglund-ship-it wants to merge 6 commits into
mainfrom
fix/refresh-on-invalid-token-review
Open

chrishagglund-ship-it wants to merge 6 commits into
mainfrom
fix/refresh-on-invalid-token-review

Conversation

@chrishagglund-ship-it

@chrishagglund-ship-it chrishagglund-ship-it commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Exploring some thoughts on making this 401 token refresh more like proposed improvement in the go-sdk, enhancement to PR #184

manan164 and others added 5 commits November 19, 2026 12:46
After a failover every in-flight request gets a 401 at once. Each one
called GetRefreshToken, which always fetches a new token under a global
lock, so N requests made N token fetches, one after another.

- RefreshToken(settings, client, staleToken) fetches only if the cache
  still holds the rejected token; otherwise it returns the cached one.
- GetToken checks the cache again under the lock, so callers that miss
  the cache together fetch once.
- A 401 on a request sent without a token (such as /token) does not
  trigger a refresh.

The existing RefreshToken and GetRefreshToken overloads are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The retry loop refreshed the token on every 401, including the last
attempt, then exited without sending a request with it. A persistent
401 made three token fetches but used only two.

Now each refresh is followed by a retry: up to
MAX_TOKEN_REFRESH_RETRY_COUNT refreshes, so up to four requests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A 401 with a body that is not JSON, such as an HTML page from a proxy
or load balancer, made the retry loop throw JsonReaderException from
the JSON parse. The caller got a parse error instead of the 401.

IsRejectedToken now treats a body it cannot parse as "not a rejected
token", so the 401 reaches the caller as ApiException with no refresh.
Both the sync and async loops use it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Drop this commit before #184 merges into main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chrishagglund-ship-it
chrishagglund-ship-it changed the base branch from main to fix/refresh-on-invalid-token November 20, 2026 16:10
The PR now targets main, so the extra branch filter is not needed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chrishagglund-ship-it
chrishagglund-ship-it changed the base branch from fix/refresh-on-invalid-token to main November 20, 2026 16:34
@chrishagglund-ship-it
chrishagglund-ship-it marked this pull request as ready for review November 20, 2026 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants