Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions metagpt/utils/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
46 changes: 46 additions & 0 deletions tests/metagpt/utils/test_report.py
Original file line number Diff line number Diff line change
@@ -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
Loading