Skip to content

Commit f871087

Browse files
author
7418
committed
fix: clear stale sdk_session_id on mid-stream crash to prevent repeated failures
When a resumed SDK session crashes mid-stream (after outputting some content), the sdk_session_id remains in the database. Every subsequent message in that chat session attempts to resume the same broken SDK session, causing repeated failures. Root cause: the SDK process starts successfully with resume, outputs some content, then crashes (exit code 1). The sdk_session_id was already stored from a previous successful turn and never gets cleared on error. Fix: in the catch block of streamClaude(), when we were resuming a session (sdkSessionId is set), clear the sdk_session_id in the database. This ensures the next message starts a fresh SDK session instead of repeatedly hitting the same broken resume. Combined with the earlier peek-based fallback (which handles failures at startup), this covers both failure modes: 1. Resume fails immediately → fallback retries without resume in the same request 2. Resume fails mid-stream → clears sdk_session_id so the NEXT request starts fresh - src/lib/claude-client.ts: import updateSdkSessionId, add sdk_session_id reset in catch block when sdkSessionId was set
1 parent 481f218 commit f871087

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/lib/claude-client.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import type { ClaudeStreamOptions, SSEEvent, TokenUsage, MCPServerConfig, Permis
1919
import { isImageFile } from '@/types';
2020
import { registerPendingPermission } from './permission-registry';
2121
import { registerConversation, unregisterConversation } from './conversation-registry';
22-
import { getSetting, getActiveProvider } from './db';
22+
import { getSetting, getActiveProvider, updateSdkSessionId } from './db';
2323
import { findClaudeBinary, findGitBash, getExpandedPath } from './platform';
2424
import os from 'os';
2525
import fs from 'fs';
@@ -747,6 +747,19 @@ export function streamClaude(options: ClaudeStreamOptions): ReadableStream<strin
747747

748748
controller.enqueue(formatSSE({ type: 'error', data: errorMessage }));
749749
controller.enqueue(formatSSE({ type: 'done', data: '' }));
750+
751+
// If we were resuming a session and it crashed mid-stream, clear the
752+
// stale sdk_session_id so the next message starts a fresh SDK session
753+
// instead of repeatedly hitting the same broken resume.
754+
if (sdkSessionId && sessionId) {
755+
try {
756+
updateSdkSessionId(sessionId, '');
757+
console.warn('[claude-client] Cleared stale sdk_session_id for session', sessionId);
758+
} catch {
759+
// best effort
760+
}
761+
}
762+
750763
controller.close();
751764
} finally {
752765
unregisterConversation(sessionId);

0 commit comments

Comments
 (0)