Repository navigation
fix(agent-sdk): stop yielding buffered SSE events after cancellation - #46733
Merged
BeautyyuYanli merged 3 commits intoNov 20, 2026
Merged
BeautyyuYanli merged 3 commits into
BeautyyuYanli merged 3 commits into
Conversation
…ed-sse-cancellation # Conflicts: # dify-agent/src/dify_agent/client/_client.py
BeautyyuYanli
approved these changes
Nov 20, 2026
BeautyyuYanli
left a comment
Contributor
There was a problem hiding this comment.
(This message is automated while I'm responsible for it.)
Thanks for fixing #46654! The core fix checking _raise_if_stream_stopped before each decoded and flushed event in both _stream_events_once and _stream_events_once_sync while keeping chunk-level checks is clean and matches the streaming contract.
I pushed two follow-up commits to this branch:
- Merged
origin/main(cbdea6ec945e) to resolve the conflict indify-agent/src/dify_agent/client/_client.pyintroduced by #46566. - Preserved already-raised
DifyAgentStreamError/DifyAgentTimeoutError(with original__cause__) when closing the HTTPX response raiseshttpx.TimeoutException,httpx.TransportError, orhttpx.StreamError, updated the streaming docstrings, and expanded the sync/async regression tests (d30ed4538d17) to cover caller cancellation,timeout_seconds, trailing\rflush, heartbeat chunks, clean EOF after the last event, and response-close failures.
All CI checks are green on d30ed4538d17. LGTM!
2 of 5 tasks
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.
Summary
Fixes #46654.
should_stopand the total stream deadline immediately before yielding every decoded SSE event.Without this check, an Agent caller that cancels between buffered events still receives later events from the same chunk. The response can then deliver work after the graph has already requested cancellation.
Validation
uv run --frozen pytest tests/local/dify_agent/client/test_client.py -q(53 passed)uv run --frozen pytest tests/local/dify_agent/client/test_client.py -k "buffered or stream_events_observes_caller_stop_on_heartbeat" -q(3 passed)uv run --frozen ruff check src/dify_agent/client/_client.py tests/local/dify_agent/client/test_client.py(passed)Prepared with AI coding assistance, reviewed by the contributor, and verified against the current upstream checkout.