Skip to content

fix(webchat): 修复WebChat SSE断开后agent未停止及队列残留问题 - #9256

Closed
NayukiChiba wants to merge 4 commits into
AstrBotDevs:masterfrom
NayukiChiba:fix/9253-webchat-sse-cancel
Closed

NayukiChiba wants to merge 4 commits into
AstrBotDevs:masterfrom
NayukiChiba:fix/9253-webchat-sse-cancel

Conversation

@NayukiChiba

@NayukiChiba NayukiChiba commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Modifications / 改动点

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

image

Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。

  • 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txt 和 pyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Improve WebChat SSE stream robustness by adding back-queue lifecycle control, stopping agents and clearing queues when clients disconnect, and updating message sending to avoid writing into closed response queues.

Bug Fixes:

  • Prevent WebChat message sending from enqueuing responses into closed back queues after SSE disconnects.
  • Ensure WebChat agents are requested to stop when clients disconnect or close streams, and clean up associated back queues.

Enhancements:

  • Introduce lifecycle management for WebChat back queues with close events to coordinate senders and cleanup.
  • Update WebChat streaming and message sending to use a guarded back-queue write API that respects queue closure.

Tests:

  • Add tests covering WebChat back-queue lifecycle, including blocked senders and queue removal behaviour.
  • Add tests verifying that WebChat chat streams request agent stop on client disconnect and stream closure.
  • Adapt existing WebChat attachment tests to the new back-queue write API.

- 为 back_queue 引入关闭事件,put_back_queue 方法在写入前检查队列有效性,避免向已关闭队列入队
- WebChat 消息事件在推送响应时对 put_back_queue 返回值进行判断,队列关闭后提前终止消息链处理
- chat_service 捕获客户端断开异常,主动调用 active_event_registry 停止对应 agent,并在 finally 块中清理 back_queue
- 新增ChatService断开与主动关闭的测试用例,覆盖agent停止请求的调用路径
- 新增back_queue移除后发方非阻塞与不复建队列的测试,验证死锁修复
- 将图像文件相关测试适配至put_back_queue新接口,确保前端断开后不再写入残留消息
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. feature:chatui The bug / feature is about astrbot's chatui, webchat labels Jul 13, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • The new put_back_queue implementation is quite complex; consider extracting the queue-write-and-close-race logic into a small internal helper or adding inline comments to clarify why asyncio.wait with two tasks is needed and how cancellation is handled to make future maintenance easier.
  • The new tests that use object.__new__(ChatService) bypass ChatService.__init__, which can make them fragile if the constructor gains required state in the future; it would be more robust to instantiate ChatService normally or provide a lightweight test-specific constructor/factory.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new `put_back_queue` implementation is quite complex; consider extracting the queue-write-and-close-race logic into a small internal helper or adding inline comments to clarify why `asyncio.wait` with two tasks is needed and how cancellation is handled to make future maintenance easier.
- The new tests that use `object.__new__(ChatService)` bypass `ChatService.__init__`, which can make them fragile if the constructor gains required state in the future; it would be more robust to instantiate `ChatService` normally or provide a lightweight test-specific constructor/factory.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the webchat queue management system to gracefully handle client disconnections by introducing a new put_back_queue method and managing queue close events. Feedback on these changes suggests checking the return value of put_back_queue in send_streaming to stop generator iteration early, and wrapping the database persistence call flush_pending_bot_message() in asyncio.shield within the finally block to ensure pending messages are saved even if the task is cancelled.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

payload["text"] = text

await web_chat_back_queue.put(payload)
await webchat_queue_mgr.put_back_queue(request_id, payload)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在 send_streaming 中处理 audio_chunk 时,未对 put_back_queue 的返回值进行判断。如果客户端已断开连接且队列已关闭,我们应该停止迭代生成器并提前返回,以避免不必要的后台处理和资源浪费。

Suggested change
await webchat_queue_mgr.put_back_queue(request_id, payload)
accepted = await webchat_queue_mgr.put_back_queue(request_id, payload)
if not accepted:
return

)
webchat_queue_mgr.remove_back_queue(message_id)
try:
await flush_pending_bot_message()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

当 SSE 流任务被取消(例如由于客户端断开连接)时,在 finally 块中直接 await flush_pending_bot_message() 会立即抛出 CancelledError,从而可能导致最后一条机器人消息无法成功持久化。使用 asyncio.shield 包裹它可以确保即使在任务取消时,挂起的消息也能成功保存到数据库中。

Suggested change
await flush_pending_bot_message()
await asyncio.shield(flush_pending_bot_message())

- 在 put_back_queue 中增加队列关闭事件竞争检测,避免满队列时发送者永久阻塞
- 当 close_event 触发时立即放弃写入并返回 False,调用方据此终止后续数据推送
- 修复消息落盘操作可能因任务取消而中断的问题,使用 asyncio.shield 保证完整性
- 新增 ChatService 构造器测试夹具,通过公共构造函数创建实例并注入必要依赖
- 重构现有流断开与关闭测试,使用夹具替代 object.__new__ 绕过初始化
- 验证聊天流被取消时,消息落盘操作仍能完整执行,不被任务取消中断
- 验证后端队列关闭时音频流及时停止,不再产生后续语音数据块
@Soulter

Soulter commented Jul 13, 2026

Copy link
Copy Markdown
Member

#9259 fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. feature:chatui The bug / feature is about astrbot's chatui, webchat size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 模型思考时刷新 ChatUI,之后发的消息被卡在队列永远得不到回复

2 participants