Skip to content

fix(core): serialize MCP OAuth refreshes across processes - #50994

Closed
rekram1-node wants to merge 1 commit into
devfrom
refresh-lock
Closed

rekram1-node wants to merge 1 commit into
devfrom
refresh-lock

Conversation

@rekram1-node

Copy link
Copy Markdown
Collaborator

Fixes #34520.

Problem

MCP OAuth credentials live in one global row that every opencode process shares: the background server, each opencode acp, and run --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 with invalid_grant. Servers with refresh-token reuse detection go further and revoke the whole token family, so every process ends up in needs_auth and the user has to log in again.

Within one process, concurrent refreshes already share a single request. Across processes, nothing coordinates them.

Change

  • SDK patch. Adds an optional withRefreshLock(refresh) hook to OAuthClientProvider. The SDK runs its read-tokens, refresh, save-tokens sequence inside that hook. Providers that don't implement it are unchanged.
  • MCP provider. The provider for a stored credential implements the hook with the existing EffectFlock lock, keyed by credential ID. Because tokens() 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

  • Parallel processes no longer knock each other into needs_auth when a shared token expires.
  • Servers with reuse detection no longer revoke the token family.

Tests

  • New test: two providers share one credential row, as two processes would, and refresh concurrently. The second refresh presents the rotated token. With the lock removed, the test fails because both refreshes present the same token.
  • packages/core: MCP, MCP OAuth and integration suites pass (116 tests). bun run check passes.

@kvnloo

kvnloo commented Sep 25, 2026

Copy link
Copy Markdown

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:

  • it constructs two separate providers sharing the same EffectFlock state directory
  • it uses plain fetch, so the providers do not share an in-memory single-flight request
  • the token endpoint deliberately holds the first refresh open
  • the observed refresh-token sequence must be r, then r+
  • the final stored refresh token must be r++

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.

@github-actions

Copy link
Copy Markdown
Contributor

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:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

V2 MCP OAuth: serialize credential refresh across locations/processes

2 participants