Repository navigation
fix(webchat): 修复WebChat SSE断开后agent未停止及队列残留问题 - #9256
NayukiChiba wants to merge 4 commits into
Conversation
- 为 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新接口,确保前端断开后不再写入残留消息
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The new
put_back_queueimplementation is quite complex; consider extracting the queue-write-and-close-race logic into a small internal helper or adding inline comments to clarify whyasyncio.waitwith two tasks is needed and how cancellation is handled to make future maintenance easier. - The new tests that use
object.__new__(ChatService)bypassChatService.__init__, which can make them fragile if the constructor gains required state in the future; it would be more robust to instantiateChatServicenormally 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.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
| ) | ||
| webchat_queue_mgr.remove_back_queue(message_id) | ||
| try: | ||
| await flush_pending_bot_message() |
There was a problem hiding this comment.
- 在 put_back_queue 中增加队列关闭事件竞争检测,避免满队列时发送者永久阻塞 - 当 close_event 触发时立即放弃写入并返回 False,调用方据此终止后续数据推送 - 修复消息落盘操作可能因任务取消而中断的问题,使用 asyncio.shield 保证完整性
- 新增 ChatService 构造器测试夹具,通过公共构造函数创建实例并注入必要依赖 - 重构现有流断开与关闭测试,使用夹具替代 object.__new__ 绕过初始化 - 验证聊天流被取消时,消息落盘操作仍能完整执行,不被任务取消中断 - 验证后端队列关闭时音频流及时停止,不再产生后续语音数据块
|
#9259 fixed |
Modifications / 改动点
closes [Bug] 模型思考时刷新 ChatUI,之后发的消息被卡在队列永远得不到回复 #9253, [Bug] ChatUI 模型思考时刷新页面,回复丢失 #9249
Screenshots or Test Results / 运行截图或测试结果
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.txtandpyproject.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:
Enhancements:
Tests: