Repository navigation
fix(core): serialize MCP OAuth refreshes across processes - #50994
rekram1-node wants to merge 1 commit into
Conversation
|
I took a closer look at the cross-process refresh regression on head 00aee1d. The useful part is that this is testing more than a normal OAuth refresh:
That pins the important ownership invariant: the waiter has to re-read the credential after acquiring the lock rather than replaying the refresh token that the previous holder just rotated. The exact-head check and test workflows are green. The remaining limit I see is that the regression uses separate provider instances sharing the filesystem lock rather than two real OS processes against an IdP with reuse detection. That feels like qualification evidence rather than a missing unit-level contract, assuming EffectFlock cross-process exclusion is already covered at its owner. |
|
Automated PR Cleanup Thank you for contributing to opencode. Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions. This PR was closed because it matched the following cleanup criteria:
PRs created within the last month are not affected by this cleanup. If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate. Thanks again for taking the time to contribute. |
Fixes #34520.
Problem
MCP OAuth credentials live in one global row that every opencode process shares: the background server, each
opencode acp, andrun --standalone. When the access token expires, two processes can refresh at once with the same refresh token. The authorization server rotates it for the first request and rejects the second withinvalid_grant. Servers with refresh-token reuse detection go further and revoke the whole token family, so every process ends up inneeds_authand the user has to log in again.Within one process, concurrent refreshes already share a single request. Across processes, nothing coordinates them.
Change
withRefreshLock(refresh)hook toOAuthClientProvider. The SDK runs its read-tokens, refresh, save-tokens sequence inside that hook. Providers that don't implement it are unchanged.EffectFlocklock, keyed by credential ID. Becausetokens()runs inside the lock, a process that waited reads the refresh token the previous holder just saved. It never replays the old one.The hook sits in the SDK because every refresh happens there, triggered by a 401, a step-up or a retry after
invalid_grant. Wrapping only our own call sites would leave those paths unlocked.A compare-and-swap on the row, as the issue suggests, would keep the stale refresh from overwriting good tokens. But the stale refresh request would still reach the authorization server, and that request is what triggers reuse detection. Only serializing the refresh stops it.
What this wins
needs_authwhen a shared token expires.Tests
packages/core: MCP, MCP OAuth and integration suites pass (116 tests).bun run checkpasses.