Skip to content

fix: resolve AI chat hang caused by main-actor-bound streaming (#735)#738

Merged
datlechin merged 3 commits intomainfrom
fix/ai-chat-hang-735
Apr 14, 2026
Merged

fix: resolve AI chat hang caused by main-actor-bound streaming (#735)#738
datlechin merged 3 commits intomainfrom
fix/ai-chat-hang-735

Conversation

@datlechin
Copy link
Copy Markdown
Collaborator

Summary

  • Move streaming loop, schema fetching, and conversation loading off @MainActor using Task.detached with explicit MainActor.run for state mutations — fixes the primary hang
  • Cap concurrent schema fetch queries to 4 (sliding-window TaskGroup) instead of firing all 20 at once against a serial driver connection
  • Consolidate JSON parsing in AnthropicProvider (3→1 parse per SSE line) and OpenAICompatibleProvider (2→1 parse per SSE line)
  • Replace per-token scroll onChange with stream-end scroll to eliminate 50-100+ layout passes/sec during streaming
  • Wrap AIProviderFactory cache with OSAllocatedUnfairLock for thread safety
  • Remove @MainActor from AISchemaContext.buildSystemPrompt — pass editorLanguage/queryLanguageName as params instead
  • Fix startStreaming not cancelling existing stream before starting a new one
  • Fix fetchSchemaContext race when tables change rapidly (store + cancel task)
  • Fix cancelStream using messages.last instead of streamingAssistantID for placeholder removal
  • Handle Anthropic in-stream error events (e.g., overloaded_error) instead of silently ignoring them

Closes #735

Test plan

  • Connect to PostgreSQL (Docker), open AI chat, send a message — UI stays responsive during streaming
  • Test with Ollama (fast local streaming) for highest token rate — no spinning cursor
  • Open AI panel with 20+ tables — schema fetch doesn't stall the UI
  • Start streaming, tap Stop mid-stream — clean cancellation, partial content preserved
  • Send a new message while previous stream is still active — old stream cancelled, new one starts cleanly
  • Switch databases rapidly — schema context updates correctly without stale data
  • App launch with saved conversations — conversations load without blocking startup
  • Test all providers: Anthropic, OpenAI, Ollama, Gemini — streaming works correctly

@datlechin datlechin merged commit 1999deb into main Apr 14, 2026
2 checks passed
@datlechin datlechin deleted the fix/ai-chat-hang-735 branch April 14, 2026 03:40
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.

Bug: Using AI chat hangs the app

1 participant