Repository navigation
fix(core): reuse MCP OAuth refresh answers for spent tokens - #53275
rekram1-node wants to merge 1 commit into
Conversation
|
While investigating #60612 I wrote a focused regression test for this: two sequential refreshes with the spent token against a server that rejects reuse, asserting one network request and the same rotated response. My now-closed #60896 had it in |
|
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. |
Summary
Refresh tokens rotate: once the token server answers a refresh, the presented refresh token is spent. Concurrent refreshes of the same token already share one request, but that request was forgotten as soon as the answer arrived, before the new tokens were saved. A connection sharing the credential that read the stored token in that window sent the spent token itself and got a 429 or
invalid_grant:needs_auth.invalid_grantasks us to drop the credential. The guard against dropping a newer token does not help when the new token has not been saved yet, so the user has to sign in again.This hits hardest when access tokens expire together and every connection refreshes at once.
Fix. A successful refresh answer is now kept for 60 seconds, keyed by the spent token. A later refresh with that token gets the same answer, including the new tokens, without contacting the server. Failed answers are forgotten immediately, so a retry still reaches the server.
This covers every connection in one process. Coordinating refreshes across processes that share credential storage is out of scope.
Verification
bun test test/mcp.test.ts test/mcp-oauth.test.tsandbun run checkpass.