From d1ee596b5b5044d96d8753cb5170039fa142518a Mon Sep 17 00:00:00 2001 From: duhan <255896073+Luckyou-d@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:21:15 +0800 Subject: [PATCH] fix(report): stop the LLM stream task when the reporter context is cancelled `ResourceReporter.__aexit__` skipped its cleanup branch when the exit reason was `asyncio.CancelledError`, and that branch is the only place that puts the `None` sentinel the stream task waits for. `_llm_stream_report` returns only after it reads that sentinel, so a cancelled context left the task blocked on `queue.get()`: dropping `self._llm_task` does not stop a task the event loop still holds. Cancel and await the task on the cancelled path, and keep the sentinel path unchanged for a normal exit. Reachable only with `enable_llm_stream=True` (the default is False). --- metagpt/utils/report.py | 16 +++++++++-- tests/metagpt/utils/test_report.py | 46 ++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 tests/metagpt/utils/test_report.py diff --git a/metagpt/utils/report.py b/metagpt/utils/report.py index 0ae19beba8..50cbd0668c 100644 --- a/metagpt/utils/report.py +++ b/metagpt/utils/report.py @@ -167,10 +167,20 @@ async def __aenter__(self): async def __aexit__(self, exc_type, exc_value, exc_tb): """Exit the asynchronous streaming callback context.""" - if self.enable_llm_stream and exc_type != asyncio.CancelledError: - await get_llm_stream_queue().put(None) - await self._llm_task + if self.enable_llm_stream and self._llm_task is not None: + task = self._llm_task self._llm_task = None + if exc_type is asyncio.CancelledError: + # The sentinel below is not reached on a cancelled exit, and `_llm_stream_report` + # only returns after it reads that sentinel, so stopping the task is on us here. + task.cancel() + try: + await task + except asyncio.CancelledError: + pass + else: + await get_llm_stream_queue().put(None) + await task await self.async_report(None, END_MARKER_NAME) async def _llm_stream_report(self, queue: asyncio.Queue): diff --git a/tests/metagpt/utils/test_report.py b/tests/metagpt/utils/test_report.py new file mode 100644 index 0000000000..dc651c40ff --- /dev/null +++ b/tests/metagpt/utils/test_report.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +"""ResourceReporter must not leave the LLM stream task behind when it is cancelled.""" + +import asyncio + +import pytest + +from metagpt.utils.report import BlockType, ResourceReporter + + +@pytest.mark.asyncio +async def test_cancelled_context_stops_the_llm_stream_task(): + """A cancellation skips the sentinel, so the task has to be cancelled and awaited.""" + reporter = ResourceReporter(block=BlockType.THOUGHT, enable_llm_stream=True, callback_url="") + + async def run() -> None: + async with reporter: + await asyncio.sleep(30) + + outer = asyncio.create_task(run()) + await asyncio.sleep(0.05) + stream_task = reporter._llm_task + assert stream_task is not None + + outer.cancel() + with pytest.raises(asyncio.CancelledError): + await outer + + # Observed inside the loop: the task must already be finished, not left on queue.get(). + assert stream_task.done() + assert reporter._llm_task is None + + +@pytest.mark.asyncio +async def test_normal_exit_still_stops_the_llm_stream_task(): + """The sentinel path is unchanged for a regular exit.""" + reporter = ResourceReporter(block=BlockType.THOUGHT, enable_llm_stream=True, callback_url="") + + async with reporter: + stream_task = reporter._llm_task + assert stream_task is not None + + assert stream_task.done() + assert not stream_task.cancelled() + assert reporter._llm_task is None