You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
OAuth refresh token lost when an RPC process is stopped during the startup catalog refresh (invalid_grant) #11692
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
Let the openai access token in auth.json expire.
pi -ne --mode rpc --no-session, send get_state, then SIGTERM it 200–300 ms after it answers.
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.
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").
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.
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.
Activity
astarktc commentedon Nov 2, 2026
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 inmodels.ts:594, 627–645.shutdown()inrpc-mode.ts:367–375, 726–742doesn't wait for it. Happy to share the test harness.It isn't specific to
openai. The same harness with an expiredanthropic(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 printedAnthropic token refresh request failed … 400 … {"error":"invalid_grant","error_description":"Refresh token not found or invalid"}. With--offlineno token request went out (0 of 10). On my machines, 18 of the 20anthropiclogins 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").github-actions commentedon Nov 2, 2026
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
lgtmion one of your issues, your future issues will stay open. If a maintainer replieslgtm, your future issues and PRs will stay open. The command must be at the start of the reply (optionally after one or more@usernamementions) or at the end.See CONTRIBUTING.md.
fix(coding-agent): wait for a started OAuth refresh to be saved befor…
petrroll commentedon Nov 4, 2026
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 realpi --mode rpcand 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()isTerminateProcess. 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 indocs/rpc.md.This comment is AI-generated by
/wrfix(coding-agent): wait for a started OAuth refresh to be saved befor…
fix(coding-agent): wait for a started OAuth refresh to be saved befor…
fix(coding-agent): stub model services in the cold-start quit test
fix(coding-agent): stub model services in the cold-start quit test
fix(coding-agent): stub model services in the cold-start quit test
fix(coding-agent): stub model services in the cold-start quit test
fix(coding-agent): wait for a started OAuth refresh to be saved befor…
fix(coding-agent): wait for a started OAuth refresh to be saved befor…
fix(deps): update dependency @earendil-works/pi-coding-agent to v1.5.…