Skip to content

[Bug] Exit-time refresh tokens not persisted, causing invalid grant errors on restart #16957

Closed as not planned

Description

@runeb

Bug Description
Claude Code fails to persist refresh tokens received during exit, causing authentication failures on restart

Description

Claude Code v2.1.1 exhibits inconsistent OAuth refresh token handling with several issues:

Issue 1: Exit-time refresh tokens not persisted

When Claude Code exits and performs a token refresh during shutdown, it does not persist the new refresh token. On restart, it attempts to use a previously rotated token that has been revoked.

Observed pattern:

  1. Claude Code exits, /exit or ctrl-c x 2 (sometimes triggers a final token refresh)
  2. Server returns a new rotated refresh_token
  3. Claude Code exits without persisting this token
  4. On restart: Claude Code uses a previous refresh token instead of the latest one
  5. Result: "Invalid grant: refresh token is invalid" because the old token was revoked

Issue 2: Inconsistent error reporting

When token refresh fails, Claude Code behavior is inconsistent:

  • Sometimes it correctly reports an authentication error (1 MCP server failed)
  • Sometimes it fails silently and shows the MCP as connected and authenticated, even though the server returned an error
  • In the silent failure case, users only discover the problem when attempting to use MCP tools: "Error: Unauthorized - Session not found"

Reproduction

Testing with accelerated token expiry consistently shows Claude Code reverting to earlier refresh tokens on restart instead of using the latest token it received.

Impact

Users experience authentication failures on restart requiring re-authentication, even though valid refresh tokens were provided by the OAuth server.

Environment Info

  • Platform: darwin
  • Terminal: ghostty
  • Version: 2.1.1
  • Feedback ID: 612db877-e9b4-40ba-823e-317d1fa05050

Errors

[]

Activity

  1. added
    bug
    has repro
    platform:macos
    on Jan 8, 2026
  2. github-actions commented on Jan 8, 2026

    @github-actions

    Found 3 possible duplicate issues:

    1. Bug: /login Does Not Recover Active Session After OAuth Token Expiration #15007
    2. Authentication token expiration causes request failures even after re-authentication #15137
    3. [BUG] Claude Code can't find refresh_token in connecting with MCP Server using OAuth #12614

    This issue will be automatically closed as a duplicate in 3 days.

    • If your issue is a duplicate, please close it and 👍 the existing issue instead
    • To prevent auto-closure, add a comment or 👎 this comment

    🤖 Generated with Claude Code

  3. runeb commented on Jan 9, 2026

    @runeb
    Author

    Not duplicates.

    This issue is specifically about oauth refresh token rotation during exit not being persisted. On restart, Claude Code uses an old rotated token that was revoked, causing "Invalid grant" errors.

    Different bugs with different reproduction patterns.

  4. github-actions commented on Feb 9, 2026

    @github-actions

    This issue has been inactive for 30 days. If the issue is still occurring, please comment to let us know. Otherwise, this issue will be automatically closed in 30 days for housekeeping purposes.

  5. added
    autoclose
    on Feb 9, 2026
  6. github-actions commented on Feb 23, 2026

    @github-actions

    Closing for now — inactive for too long. Please open a new issue if this is still relevant.

  7. github-actions commented on Mar 3, 2026

    @github-actions

    This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.

  8. locked as resolved and limited conversation to collaborators on Mar 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:authautoclosebughas reproplatform:macos

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions