Skip to content

fix(claude-code): clean up all session state to prevent "already in use" errors#773

Open
bdevore17 wants to merge 1 commit intocoder:mainfrom
bdevore17:fix/stale-session-cleanup
Open

fix(claude-code): clean up all session state to prevent "already in use" errors#773
bdevore17 wants to merge 1 commit intocoder:mainfrom
bdevore17:fix/stale-session-cleanup

Conversation

@bdevore17
Copy link

Summary

  • Adds cleanup_stale_session() function that removes all Claude CLI session state files (projects, debug, todos, session-env) for a given session ID
  • Calls cleanup before starting a new task session when is_valid_session() fails, preventing "Session ID already in use" errors from leftover metadata

Problem

When a Claude session fails (e.g., due to auth errors, OOM, pod eviction), the start script only checks/cleans up the .jsonl transcript 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

  • Simulate a failed session (e.g., invalid API key) on first boot, then restart the workspace — verify it recovers instead of failing with "Session ID already in use"
  • Verify normal first-boot flow still works (no session state to clean up)
  • Verify resume flow still works when a valid session exists

Fixes #726

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

claude-code: stale session state causes "Session ID already in use" after workspace restart

1 participant