Skip to content

fix(agent-sdk): close async SSE responses promptly - #46566

Merged
BeautyyuYanli merged 2 commits into
langgenius:mainfrom
Wang-Zephyr:fix/agent-sdk-close-async-sse-response
Nov 19, 2026
Merged

BeautyyuYanli merged 2 commits into
langgenius:mainfrom
Wang-Zephyr:fix/agent-sdk-close-async-sse-response

Conversation

@Wang-Zephyr

Copy link
Copy Markdown
Contributor

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

  • Three regression cases failed on the base with an open response: terminal delivery, cancel_run_and_wait, and explicit iterator close.
  • All 52 tests passed with 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.
  • Ruff formatting and package lint, package Pyrefly, and the baseline-aware test-doubles guard passed.

Checklist

  • Linked the issue and verified the regression against the base and fix.

From Codex

@github-actions github-actions Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Nov 17, 2026

@BeautyyuYanli BeautyyuYanli 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.

(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 wrap Client.cancel_run_and_wait with contextlib.aclosing(...) across early exit paths;
  • tighten async SSE lifetime tests with an open httpx.AsyncByteStream fixture and multi-frame post-terminal coverage for until_terminal=False, and clarify the Client.stream_events docstring/comments.

@BeautyyuYanli
BeautyyuYanli added this pull request to the merge queue Nov 19, 2026
Merged via the queue into langgenius:main with commit 64fab8e Nov 19, 2026
38 checks passed
@dosubot

dosubot Bot commented Nov 19, 2026

Copy link
Copy Markdown

📄 Knowledge review

✏️ Documentation updates

1 page was updated by changes in this PR.

Page Library Status
Dify Agent Server 模块分析 (Commit 55f95dbc) dify ✅ Updated
📝 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))。
 
 ***
 

Leave Feedback Ask Dosu about dify

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

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Async Dify Agent SDK leaves SSE responses open after terminal delivery or iterator close

2 participants