Skip to content

fix: preserve session on timeout instead of destroying it#163

Open
gabmfranco-ds wants to merge 2 commits intoRichardAtCT:mainfrom
gabmfranco-ds:fix/preserve-session-on-timeout
Open

fix: preserve session on timeout instead of destroying it#163
gabmfranco-ds wants to merge 2 commits intoRichardAtCT:mainfrom
gabmfranco-ds:fix/preserve-session-on-timeout

Conversation

@gabmfranco-ds
Copy link

Summary

  • Bug: When Claude SDK times out during a session resume, the bot treats it as "session expired" and destroys the session. The next user message starts a fresh session with no conversation history, causing the bot to say "I don't have context about what we were doing."
  • Fix: Catch ClaudeTimeoutError separately from other resume errors. Timeouts are transient — the Claude session is still valid on the CLI side. The session is preserved so auto-resume works on the next message.
  • Timeout bump: Default timeout increased from 300s to 1200s (20 min) to better support long-running agentic operations.

Root cause

In facade.py, the except Exception block (line 92) caught all errors during session resume — including timeouts — and ran the "stale session" recovery path: delete session → create new → retry without history. But ClaudeTimeoutError ≠ session expired. The session is likely still alive on Claude's side; it just took too long to respond.

Changes

File Change
src/claude/facade.py Catch ClaudeTimeoutError before the generic Exception handler; preserve session and touch last_used to prevent expiration
src/utils/constants.py DEFAULT_CLAUDE_TIMEOUT_SECONDS: 300 → 1200

Test plan

  • Send a message that triggers a long-running Claude operation
  • Verify that after timeout, the next message resumes the same session (not a fresh one)
  • Verify that a genuine session-expired error still creates a fresh session (existing behavior preserved)

🤖 Generated with Claude Code

gabmfranco-ds and others added 2 commits March 23, 2026 09:58
Timeouts are transient — the Claude session is still valid on the CLI
side. Previously, any error during resume (including timeout) would
delete the session and start fresh, causing the bot to lose all
conversation context.

Now ClaudeTimeoutError is caught separately and the session is kept
intact so auto-resume works on the next message. Also bumps default
timeout from 300s to 1200s for long-running operations.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Previously, any error during session resume (timeout, process crash,
connection issue) would destroy the session and start fresh. Now the
session is only destroyed when the error message explicitly says the
session is gone ("session not found", "invalid session", etc.).

All other errors preserve the session and touch last_used so
auto-resume works on the next user message.

Also updates .env CLAUDE_TIMEOUT_SECONDS from 600 to 1200 (the
constants.py default is overridden by the env var).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.

1 participant