Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
fix(auth): prevent token refresh race conditions causing refresh_token_reused errors
Antigravity Assistant7ec572ca47f7
diff --git a/open-sse/services/tokenRefresh.ts b/open-sse/services/tokenRefresh.tsindex e1324acced..0b9edc7bfc 100755--- a/open-sse/services/tokenRefresh.ts+++ b/open-sse/services/tokenRefresh.ts@@ -841,6 +841,51 @@ export async function getAccessToken(provider, credentials, log, proxyConfig: unreturn null;}+ // RACE CONDITION PREVENTION:+ // If the credentials object in memory is stale (e.g. it waited in a semaphore while another+ // request refreshed the token), using its OLD refreshToken will cause the provider (e.g. OpenAI)+ // to reject it with 'refresh_token_reused' and revoke the new token family.+ // We MUST check if the DB has a newer token before proceeding with a network refresh.+ if (credentials.connectionId) {+ try {+ const { getProviderConnectionById } = await import("../../src/lib/db/providers");+ const dbConnection = await getProviderConnectionById(credentials.connectionId);+ if (+ dbConnection &&+ dbConnection.refreshToken &&+ dbConnection.refreshToken !== credentials.refreshToken+ ) {+ log?.info?.(+ "TOKEN_REFRESH",+ `Stale token detected in memory for ${provider}. Using refreshed token from DB.`+ );++ // If the DB token is not expired, we can just return it!+ const now = Date.now();+ const dbExpiresAt = dbConnection.expiresAt ? new Date(dbConnection.expiresAt).getTime() : 0;++ if (dbExpiresAt > now + 60000) {+ // 60 seconds buffer+ log?.info?.("TOKEN_REFRESH", `DB token is still valid. Skipping OAuth refresh.`);+ return {+ accessToken: dbConnection.accessToken,+ refreshToken: dbConnection.refreshToken,+ expiresIn: dbConnection.expiresIn,+ };+ } else {+ // DB token is also expired, but it's the NEWEST one. We must use it to refresh.+ credentials.refreshToken = dbConnection.refreshToken;+ credentials.accessToken = dbConnection.accessToken;+ }+ }+ } catch (e) {+ log?.warn?.(+ "TOKEN_REFRESH",+ `Failed to check DB for stale token: ${e instanceof Error ? e.message : String(e)}`+ );+ }+ }+const cacheKey = getRefreshCacheKey(provider, credentials.refreshToken);// If a refresh is already in-flight, reuse it