dify icon indicating copy to clipboard operation
dify copied to clipboard

Agent stream close leaves a failed read-ahead task unhandled

Open Wang-Zephyr opened this issue 2 weeks ago • 2 comments

Self Checks

  • [x] I read the contribution guide and language policy.
  • [x] This is a bug report.
  • [x] I searched existing issues, including closed issues.
  • [x] The report is in English.
  • [x] The required fields are filled in.

Dify version

Dify Agent 1.19.0 on main at 7688f7c9b1b46e3c813eccf52bcdc86c1f4f1161.

Cloud or Self Hosted

Self-hosted, source checkout with Python 3.13.

Steps to reproduce

  1. Drive the Agent event coalescer with a source that emits a PartEndEvent, then raises a RuntimeError on the subsequent read.
  2. Consume the first event and let the coalescer's read-ahead task finish with that error.
  3. Close the stream with aclose() without requesting a second event. Capture the event loop's exception-handler messages.

✔️ Expected Behavior

Closing an abandoned stream collects the result of the pending or completed read-ahead task. Errors still reach a consumer that continues reading.

❌ Actual Behavior

Closing succeeds, but the asyncio event loop reports Task exception was never retrieved. The stream retained the failed read-ahead task until close and released it without retrieving its exception.

Wang-Zephyr avatar Nov 16 '26 11:11 Wang-Zephyr

The coalescer’s cleanup guard matches this failure: it schedules the next anext(iterator) before yielding the non-text event, but its finally only awaits that task when not next_event_task.done(). A read-ahead task that has already failed is therefore skipped during aclose(), leaving its exception unretrieved. This is source inspection, not an executed reproduction. [1] [2]

The cleanup should collect any retained task’s outcome, whether pending or completed, cancelling only if still pending. An abandoned read’s failure needs to be handled at that cleanup boundary without changing the normal reading path, where next_event_task.result() propagates source errors. [3] [2]

The existing regression test covers flushing buffered text before propagating a source failure, but not closing after a non-text yield with an already-failed read-ahead task. Your reproduction would add that missing case; a companion case should confirm that continuing with anext() still raises the original RuntimeError. [4]

To reply, just mention @dosu.


Share context across your team and agents. Try Dosu.

Leave Feedback Learn about dify with Dosu

dosubot[bot] avatar Nov 16 '26 11:11 dosubot[bot]

Confirmed, when aclose() tears down an early-terminated stream, dropping or canceling _read_ahead_task leaves the finished task's exception uninspected if it already failed in the background, triggering the event loop warning on GC.

In the coalescer's aclose() cleanup, checking task completion and consuming task.exception() resolves this cleanly:

Patch Preview (First 3 lines of verified fix):

async def aclose(self) -> None:
    if self._read_ahead_task is not None:
        task = self._read_ahead_task
# ... [TRUNCATED — Full production patch & verification steps locked below]

grinem avatar Nov 16 '26 12:11 grinem