Skip to content

fix(openai): refresh a rejected OAuth access token on 401 and resend once - #9013

Open
Anankke wants to merge 1 commit into
Wei-Shaw:mainfrom
Anankke:fix/openai-oauth-401-refresh
Open

Anankke wants to merge 1 commit into
Wei-Shaw:mainfrom
Anankke:fix/openai-oauth-401-refresh

Conversation

@Anankke

@Anankke Anankke commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Fixes #5542

The refresh and resend sit in doOpenAIUpstream under the OAuthRefreshAPI locks, so every route that sends through it is covered.

Concurrent 401s with the same token on one instance share one refresh attempt, so a failed refresh is not repeated by every request in the burst. If another instance holds the refresh lock, the request rereads the stored token up to five times with the backoff the token provider already uses for that case and then takes main's 401 path, so a stuck lock cannot stall it. Not refreshed, because a refresh does not fix them: 401 bodies main treats as permanent, deactivation, missing scopes, and a token that has not worked since it was minted if this path minted it or the account has been cooled down since.

#7826 and #8034 retry in Forward and passthrough and leave a failed refresh to main's 401 handling; #8034 also retries the WebSocket handshake once. #8856 retries in Forward, passthrough and the account tests and sets the account to error on a non-retryable refresh error, as this PR does.

…once

On a 401 before expires_at, an OpenAI OAuth account was cooled down and kept the same token until the periodic refresher reached it.

doOpenAIUpstream now refreshes the rejected token under the OAuthRefreshAPI locks and resends once. Concurrent 401s with the same token on one instance share the attempt. While another instance holds the refresh lock, the stored token is reread up to openAILockMaxAttempts times; after that, or when the lock backend errors, the existing 401 handling applies. A non-retryable refresh error blocks scheduling and sets the account to error, as the periodic refresher does.

Not refreshed: 401 bodies already treated as permanent, deactivation, missing scopes, and a token that has not worked since it was minted if this path minted it or the account has been cooled down since.
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.

[Bug] OpenAI OAuth上游返回401后未自动刷新Token,账号被临时停止调度

1 participant