fix(claude-code): clean up all session state to prevent "already in use" errors#773
Open
bdevore17 wants to merge 1 commit intocoder:mainfrom
Open
fix(claude-code): clean up all session state to prevent "already in use" errors#773bdevore17 wants to merge 1 commit intocoder:mainfrom
bdevore17 wants to merge 1 commit intocoder:mainfrom
Conversation
…se" errors Claude CLI tracks session state across multiple directories (projects, debug, todos, session-env), not just the .jsonl transcript file. When a session fails (e.g., due to auth errors) and the start script retries with the same hardcoded session ID, it only cleaned up the .jsonl file. Claude CLI would still find leftover metadata and reject the session ID as "already in use", causing the agent to exit immediately. Add a cleanup_stale_session() function that removes all session state files across all directories before starting a new session. This is called when is_valid_session() determines the existing session is invalid and needs to be replaced. Fixes coder#726
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
cleanup_stale_session()function that removes all Claude CLI session state files (projects, debug, todos, session-env) for a given session IDis_valid_session()fails, preventing "Session ID already in use" errors from leftover metadataProblem
When a Claude session fails (e.g., due to auth errors, OOM, pod eviction), the start script only checks/cleans up the
.jsonltranscript file. However, Claude CLI tracks session state across multiple directories:~/.claude/projects/<dir>/<session-id>.jsonl~/.claude/projects/<dir>/<session-id>/(lock directory)~/.claude/debug/<session-id>.txt~/.claude/todos/<session-id>*.json~/.claude/session-env/<session-id>When the script retries with the same hardcoded session ID (
cd32e253-...), Claude CLI finds leftover metadata and rejects it as "already in use", causing agentapi to exit immediately and return a 502 to the user.Test plan
Fixes #726