Skip to content

Doc Chat: Clear operation causes infinite 'Connecting...' loop due to stale SSE pump race condition #74

@bruceding

Description

@bruceding

问题描述

在 Doc Chat 中点击 Clear Chat 后,前端会卡在 "Connecting..." 状态,Network 面板显示多个 /stream 请求同时 pending,只有一条收到 session_initializing,其余永远连接中。

根因

processSSEStream 的 pump 函数在 done 分支和 catch 块中无条件执行了 abortRef.current = nullscheduleReconnect()。当用户点击 Clear Chat 时:

  1. startNewSession(true) 创建新的 AbortController 并发起 /stream?fresh=1
  2. 旧的 pump 在异步 cleanup 时把 abortRef 置 null,导致新连接的 watchdog 失去保护
  3. 旧 pump 同时调用 scheduleReconnect(),spawn 第二条 /stream 连接
  4. 两条连接互相竞争,用户看到永远 "Connecting..."

修复

pump()done 分支和 catch 块中,只在 abortRef.current === controller 时才执行 cleanup(清空 abortRef 和 scheduleReconnect)。这样过时的旧 pump 不会影响新连接。

涉及文件

frontend/src/components/DocumentChatPanel.tsx - processSSEStream 函数的 pump 回调

修复内容

pump() done 分支:

// Before:
abortRef.current = null
scheduleReconnect()

// After:
if (abortRef.current === controller) {
  abortRef.current = null
  scheduleReconnect()
}

startNewSession catch 块:

// Before:
scheduleReconnect()

// After:
if (abortRef.current === controller) {
  scheduleReconnect()
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions