Repository navigation
fix(agent-sdk): close async SSE responses promptly - #46566
Merged
BeautyyuYanli merged 2 commits intoNov 19, 2026
Merged
BeautyyuYanli merged 2 commits into
BeautyyuYanli merged 2 commits into
Conversation
BeautyyuYanli
approved these changes
Nov 18, 2026
BeautyyuYanli
left a comment
Contributor
There was a problem hiding this comment.
(This message is automated while I'm responsible for it.)
Reviewed and verified against #46565. Pushed a small follow-up commit (30ea38c4e239) to:
- prevent HTTPX cleanup errors raised during caller-driven interruption (
aclose(),athrow(), or task cancellation) from entering the reconnect loop, and wrapClient.cancel_run_and_waitwithcontextlib.aclosing(...)across early exit paths; - tighten async SSE lifetime tests with an open
httpx.AsyncByteStreamfixture and multi-frame post-terminal coverage foruntil_terminal=False, and clarify theClient.stream_eventsdocstring/comments.
This was referenced Nov 18, 2026
📄 Knowledge review✏️ Documentation updates1 page was updated by changes in this PR.
📝 Dify Agent Server 模块分析 (Commit 55f95db) — changes@@ -725,6 +725,8 @@
**终端事件后的流关闭行为**:服务器在交付终端事件(run_succeeded、run_failed 或 run_cancelled)后正常关闭 SSE 响应。客户端必须在消费终端事件后停止重连。`after` 查询参数和 `Last-Event-ID` 头均为排他性恢复游标,因此服务器不会重新发送由提供的游标已排除的终端事件。
> Python 客户端库(`dify_agent.client`)对 SSE 端点实现了带重连的完整封装:瞬时流/连接/读取失败、超时和 HTTP 5xx 响应将携带最后已知事件 ID 重连;HTTP 4xx、DTO 验证失败和格式错误的帧则立即抛出异常。客户端的 `stream_events()` 和 `stream_events_sync()` 方法跟踪终端事件,在观察到终端事件后不再重连,即使响应正常结束或引发可重连的传输错误。
+>
+> 异步 `stream_events()` 返回异步生成器。默认 `until_terminal=True` 时,它先关闭当前 HTTP 响应,再产出终端事件,随后结束迭代;已捕获的终端事件不会因响应清理时的超时或流错误而丢失。`until_terminal=False` 时可继续消费当前响应的剩余事件,但观察到终端事件后不再重连。提前停止消费时,调用者应使用 `await events.aclose()` 或 `contextlib.aclosing` 及时释放响应;调用者驱动的清理不会触发重连,其中的 HTTPX 超时或传输/流错误分别转换为 `DifyAgentTimeoutError` 或 `DifyAgentStreamError`。异步 `cancel_run_and_wait()` 在返回、异常和任务取消时均关闭事件迭代器(见 [`_client.py`](https://github.com/langgenius/dify/blob/main/dify-agent/src/dify_agent/client/_client.py))。
***
|
This was referenced Nov 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #46565
Summary
Make each async SSE iteration own its HTTP response. The default terminal path releases that response before yielding the terminal event, and closing the outer iterator promptly closes its active response. The opt-out path still reads to the end of the current response and the existing cursor and reconnect rules remain in place.
Verification
cancel_run_and_wait, and explicit iterator close.uv run --no-sync pytest -q tests/local/dify_agent/client/test_client.py. The response-lifetime checks use an actual HTTPX response and a local mock transport.Checklist
From Codex