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
claude-code: stale session state causes "Session ID already in use" after workspace restart #726
When a workspace is terminated unexpectedly (pod killed, node drained, etc.), Claude's session state files persist on storage. On restart, the start.sh script detects the existing session file via task_session_exists + is_valid_session and uses --resume, but Claude may reject the session as conflicting if other stale metadata files are in an inconsistent state.
Additionally, if the session file exists but is in a state that passes validation yet Claude considers it "in use" (e.g. incomplete shutdown), there is no recovery path — the script has no mechanism to clean up stale session metadata and retry with a fresh session.
Root Cause
Claude CLI creates multiple files to track session state:
~/.claude/session-env/<session-id> — environment state
The current start.sh script only checks for and validates the .jsonl session transcript file (task_session_exists and is_valid_session). It does not:
Clean up stale metadata files in ~/.claude/todos/, ~/.claude/debug/, or ~/.claude/session-env/ on startup
Handle the case where --resume fails (no fallback to starting a fresh session)
Use any form of process-level lock (e.g. flock) to distinguish between an active session and leftover files from a dead process
Suggested Fix
Add stale session cleanup on startup: Before attempting to resume, remove orphaned metadata files (todos, debug, session-env) for the hardcoded TASK_SESSION_ID when no Claude process is running.
Add fallback logic: If --resume fails, fall back to removing the stale session file and starting fresh with --session-id.
Consider flock-based locking: Use file locking to detect whether a session is genuinely active vs. leftover from a killed process.
Workaround
Manually remove stale session metadata before starting:
Activity
fix(claude-code): clean up all session state to prevent "already in u…
feat(claude-code): add transcript_retention_days and Stop-hook idle s…
35C4n0r commentedon May 28, 2026
resolved in #803