Skip to content

OAuth refresh token lost when an RPC process is stopped during the startup catalog refresh (invalid_grant) #11692

Closed

Description

@astarktc

What happened?

My openai (Sign in with ChatGPT) login keeps dying with OAuth refresh failed for openai: OpenAI OAuth token request failed (400): {"error": "invalid_grant"}. /login fixes it until the token next expires while I'm not using it.

I run pi inside T3 Code, which starts pi --mode rpc every few minutes to check status and stops it as soon as it answers. In RPC mode pi starts the catalog refresh in the background at startup, and that also refreshes any expired OAuth credential, even though the catalog doesn't need it. If the process is stopped after the token request goes out but before the new token is written, OpenAI has already used up the old refresh token and auth.json keeps it. The SIGTERM shutdown doesn't wait for the refresh (on Windows it's a hard kill anyway). This may explain some of the "dies after idle" reports in #10377.

Steps to reproduce

  1. Let the openai access token in auth.json expire.
  2. pi -ne --mode rpc --no-session, send get_state, then SIGTERM it 200–300 ms after it answers.
  3. The next request fails with invalid_grant.

Against a local token endpoint with single-use refresh tokens and ~150 ms latency, this lost the token 30 of 30 times. Stopping within 150 ms or after 350 ms never did, and neither did --offline.

Expected behavior

The catalog refresh shouldn't rotate credentials it doesn't use, and shutdown should wait (with a timeout) for a refresh already in flight. Using --offline as a workaround for now.

Version

1.4.0 (macOS and Windows, Node 26)

Activity

  1. astarktc commented on Nov 2, 2026

    @astarktc
    Author

    AI-written: an agent traced this through the source for me. The startup catalog refresh is started in main.ts:939–946. The expired-credential refresh happens inside it in models.ts:594, 627–645. shutdown() in rpc-mode.ts:367–375, 726–742 doesn't wait for it. Happy to share the test harness.

    It isn't specific to openai. The same harness with an expired anthropic (Claude subscription) login, single-use refresh tokens and a stop while the startup refresh is in flight lost the rotated token 10 of 10 times, with or without extensions; the next request printed Anthropic token refresh request failed … 400 … {"error":"invalid_grant","error_description":"Refresh token not found or invalid"}. With --offline no token request went out (0 of 10). On my machines, 18 of the 20 anthropic logins lost since August were this error, all after the RPC host started probing pi. Claude Code fixed the same class of bug on its side in 2.1.265 ("sessions occasionally requiring re-login when a session was closed while refreshing its token").

  2. github-actions commented on Nov 2, 2026

    @github-actions
    Contributor

    This issue was auto-closed. All issues from new contributors are auto-closed by default.

    Maintainers review auto-closed issues daily and reopen worthwhile ones. Issues that do not meet the quality bar in CONTRIBUTING.md will not be reopened or receive a reply.

    If a maintainer replies lgtmi on one of your issues, your future issues will stay open. If a maintainer replies lgtm, your future issues and PRs will stay open. The command must be at the start of the reply (optionally after one or more @username mentions) or at the end.

    See CONTRIBUTING.md.

  3. added
    untriagedThis issue has been auto closed and has not been triaged
    on Nov 2, 2026
  4. removed
    untriagedThis issue has been auto closed and has not been triaged
    on Nov 4, 2026
  5. added a commit that references this issue on Nov 4, 2026
    96cb327
  6. petrroll commented on Nov 4, 2026

    @petrroll
    Collaborator

    Fixed in #11812.

    The catalog refresh at startup refreshed the expired OAuth token, and RPC shutdown did not wait for it. If pi exited after the token request went out but before the new token was written, auth.json kept the spent refresh token.

    Pi now waits for credential writes in progress before it exits (AgentSessionRuntime.dispose()), in RPC, print and interactive mode. Reproduced with a real pi --mode rpc and SIGTERM during the refresh: the token was lost 3 of 3 times before, saved 3 of 3 times after.

    On Windows nothing can save a token in flight if the process is killed: child.kill() is TerminateProcess. To stop pi gracefully there (and anywhere else), close its stdin first and wait for it to exit; kill it only if it is still running after some seconds. That is the documented orderly shutdown in docs/rpc.md.

    This comment is AI-generated by /wr

  7. added a commit that references this issue on Nov 4, 2026
    b2cb629
  8. added a commit that references this issue on Nov 7, 2026
    7693390
  9. added a commit that references this issue on Nov 11, 2026
    42d290a
  10. added a commit that references this issue on Nov 12, 2026
    91e9e7f
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions