Skip to content

fix(opencode): serialize mcp-auth.json writes to prevent concurrent corruption - #29820

Closed
JunJieLiu51520 wants to merge 2 commits into
anomalyco:devfrom
JunJieLiu51520:fix/mcp-auth-concurrent-write
Closed

JunJieLiu51520 wants to merge 2 commits into
anomalyco:devfrom
JunJieLiu51520:fix/mcp-auth-concurrent-write

Conversation

@JunJieLiu51520

@JunJieLiu51520 JunJieLiu51520 commented May 29, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #29804

Type of change

  • Bug fix

What does this PR do?

McpAuth.set() and remove() used an unguarded read-modify-write pattern. Concurrent token refreshes or client info saves could race, causing one write to overwrite another — manifesting as extra } characters appended to mcp-auth.json, breaking JSON parsing and disconnecting all remote MCP servers.

Added an Effect.Semaphore(1) so all file mutations are serialised, ensuring each read-modify-write cycle sees the latest file state.

How did you verify your code works?

Added 4 tests to test/mcp/auth.test.ts:

  • "serializes concurrent auth file updates across service instances" — two service instances race to write tokens and client info; both writes survive and file stays valid JSON
  • "serializes concurrent token and client info updates within a single service instance" — token refresh races with client info save on the same instance; no data lost
  • "preserves all entries when multiple servers are updated concurrently" — 3 different servers updated simultaneously; all entries survive
  • "remove is serialized with concurrent updates" — remove races with updateTokens; deletion and update both take effect correctly

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

…orruption

McpAuth.set() and remove() used an unguarded read-modify-write pattern.
Concurrent token refreshes or client info saves could race, causing one write
to overwrite another and potentially corrupt mcp-auth.json.

Add a Semaphore(1) so all file mutations are serialised, ensuring each
read-modify-write cycle sees the latest file state.

Closes anomalyco#29804
Verify that concurrent updateTokens, updateClientInfo, and remove
calls do not corrupt mcp-auth.json and that all writes survive
without data loss.
@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.

OAuth MCP (PostHog) corrupts mcp-auth.json — extra } appended on every token refresh

1 participant