Skip to content

fix(chat): 修复 SSE 流异常处理导致的二次错误 - #113

Open
IfYou66 wants to merge 1 commit into
nageoffer:mainfrom
IfYou66:lym/fix-sse-error-handling
Open

fix(chat): 修复 SSE 流异常处理导致的二次错误#113
IfYou66 wants to merge 1 commit into
nageoffer:mainfrom
IfYou66:lym/fix-sse-error-handling

Conversation

@IfYou66

@IfYou66 IfYou66 commented Aug 12, 2026

Copy link
Copy Markdown

问题背景

流式对话发生业务异常时,当前逻辑会调用 SseEmitter.completeWithError()

该方法会触发一次异步错误分发。由于响应类型已经被设置为 text/event-stream,后续全局异常处理返回的普通 JSON 响应无法按照 SSE 类型写出,进而产生 HttpMessageNotWritableException 等二次异常。

与此同时,前端无法收到已经支持的 error SSE 事件,只能感知到连接异常结束。

修复内容

  • 新增 SSE error 事件类型。
  • 新增流式错误事件载荷,仅返回面向用户的固定错误提示。
  • 流式业务异常发生时:
    • 在服务端记录原始异常;
    • 注销流式任务;
    • 向客户端发送 error 事件;
    • 使用 complete() 正常结束 SSE 响应。
  • SSE 发送发生 IOException 时,仅标记连接关闭,不再调用 completeWithError(),避免触发二次错误分发。
  • 增加针对业务异常和客户端断连场景的回归测试。

影响范围

本次修改仅影响流式对话的异常结束路径。

正常消息、思考过程、完成事件、取消事件及队列限流逻辑保持不变。

测试

相关测试类共 4 个,全部通过:

  • SseEmitterSenderTest
    • 验证客户端断连后不会调用 completeWithError()
    • 验证连接关闭后不会继续发送事件。
  • StreamChatEventHandlerTest
    • 验证业务异常会发送 error SSE 事件。
    • 验证响应不会暴露原始异常信息。
    • 验证任务注销并正常结束 SSE 响应。
  • StreamChatPipelineTest
  • ChatQueueLimiterTest

@IfYou66
IfYou66 force-pushed the lym/fix-sse-error-handling branch from 6b19cc8 to a2a4862 Compare August 13, 2026 02:09
@IfYou66
IfYou66 marked this pull request as ready for review August 13, 2026 02:10
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.

1 participant