Skip to content

fix(agent-sdk): stop yielding buffered SSE events after cancellation - #46733

Merged
BeautyyuYanli merged 3 commits into
langgenius:mainfrom
uhhgxg:fix/dify-agent-buffered-sse-cancellation
Nov 20, 2026
Merged

BeautyyuYanli merged 3 commits into
langgenius:mainfrom
uhhgxg:fix/dify-agent-buffered-sse-cancellation

Conversation

@uhhgxg

@uhhgxg uhhgxg commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #46654.

  • Re-check should_stop and the total stream deadline immediately before yielding every decoded SSE event.
  • Apply the same guard to events emitted by the final decoder flush.
  • Cover synchronous and asynchronous clients when multiple events are buffered in one HTTP response chunk.

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.

@github-actions github-actions Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Nov 19, 2026
@github-actions github-actions Bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Nov 20, 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.)

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 in dify-agent/src/dify_agent/client/_client.py introduced by #46566.
  • Preserved already-raised DifyAgentStreamError / DifyAgentTimeoutError (with original __cause__) when closing the HTTPX response raises httpx.TimeoutException, httpx.TransportError, or httpx.StreamError, updated the streaming docstrings, and expanded the sync/async regression tests (d30ed4538d17) to cover caller cancellation, timeout_seconds, trailing \r flush, heartbeat chunks, clean EOF after the last event, and response-close failures.

All CI checks are green on d30ed4538d17. LGTM!

@BeautyyuYanli
BeautyyuYanli added this pull request to the merge queue Nov 20, 2026
Merged via the queue into langgenius:main with commit d9622af Nov 20, 2026
38 checks passed
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.

[Bug] Dify Agent SDK yields buffered SSE events after caller cancellation or deadline

2 participants