fix(oauth): wire refresh mutex into provider.refresh() for concurrent deduplication

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
YeonGyu-Kim
2026-04-08 17:16:28 +09:00
parent 06b825dd74
commit 63ba16bcce
2 changed files with 156 additions and 8 deletions
@@ -1,5 +1,6 @@
import type { ClaudeCodeMcpServer } from "../claude-code-mcp-loader/types"
import { McpOAuthProvider } from "../mcp-oauth/provider"
import { withRefreshMutex } from "../mcp-oauth/refresh-mutex"
import type { OAuthTokenData } from "../mcp-oauth/storage"
import { isStepUpRequired, mergeScopes } from "../mcp-oauth/step-up"
import type { OAuthProviderFactory, OAuthProviderLike } from "./types"
@@ -52,14 +53,15 @@ export async function buildHttpRequestInit(
}
}
if (tokenData && isTokenExpired(tokenData)) {
try {
tokenData = tokenData.refreshToken
? await provider.refresh(tokenData.refreshToken)
: await provider.login()
} catch {
if (tokenData && isTokenExpired(tokenData)) {
try {
tokenData = await provider.login()
const refreshToken = tokenData.refreshToken
tokenData = refreshToken
? await withRefreshMutex(config.url, () => provider.refresh(refreshToken))
: await provider.login()
} catch {
try {
tokenData = await provider.login()
} catch {
tokenData = null
}
@@ -149,7 +151,8 @@ export async function handlePostRequestAuthError(params: {
refreshAttempted.add(config.url)
try {
await provider.refresh(tokenData.refreshToken)
const refreshToken = tokenData.refreshToken
await withRefreshMutex(config.url, () => provider.refresh(refreshToken))
return true
} catch {
return false