Skip to content

fix(report): stop the LLM stream task when the reporter context is cancelled - #2212

Open
Luckyou-d wants to merge 1 commit into
FoundationAgents:mainfrom
Luckyou-d:fix/reporter-cancel-llm-stream
Open

Luckyou-d wants to merge 1 commit into
FoundationAgents:mainfrom
Luckyou-d:fix/reporter-cancel-llm-stream

Conversation

@Luckyou-d

Copy link
Copy Markdown

Problem

ResourceReporter.__aexit__ skipped its cleanup branch when the exit reason was asyncio.CancelledError:

if self.enable_llm_stream and exc_type != asyncio.CancelledError:
    await get_llm_stream_queue().put(None)
    await self._llm_task
    self._llm_task = None

That branch is the only place that puts the None sentinel, and _llm_stream_report returns only after it reads that sentinel. So on a cancelled exit the task stayed blocked on queue.get(). Setting self._llm_task = None would not have helped either: the event loop keeps the task alive.

Reproduced (in-loop observation, enable_llm_stream=True): after the context task is cancelled and awaited, the stream task still reports done() == False.

Change

__aexit__ now always stops the task when there is one:

  • cancelled exit - task.cancel() and await it, swallowing its own CancelledError;
  • normal exit - the sentinel path, unchanged.

The else branch keeps the previous put(None) + await order, and self._llm_task is cleared in both branches.

Verification

  • New tests/metagpt/utils/test_report.py: the cancelled-exit test fails on main (assert False on stream_task.done()) and passes with the change; the normal-exit test pins that the sentinel path is unchanged (still not cancelled, still done).
  • The repro was also confirmed standalone before writing the test: inner done? False cancelled? False (leak) on main -> inner done? True cancelled? True (no leak) with the patch.
  • Local environment note (honest): metagpt/_compat.py imports semantic_kernel.orchestration.sk_function on Windows only, and requirements.txt pins semantic-kernel==0.4.3.dev0, which does not install here. The test file itself only imports metagpt.utils.report; I ran it with a local sitecustomize stub for that module plus --confcutdir=tests/metagpt/utils (tests/conftest.py pulls in the whole provider stack). Neither workaround is part of the diff, and the Linux CI never takes the Windows branch in _compat.py.
  • Not covered: whether a cancelled exit should still await self.async_report(None, END_MARKER_NAME) (the last line). Its behaviour is unchanged by this PR; if maintainers prefer skipping the end marker on cancellation, that is a separate decision.

…ncelled

`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).
@Luckyou-d

Copy link
Copy Markdown
Author

About the one failing check: build (3.9) fails with

##[error]Refusing to check out fork pull request code from a 'pull_request_target' workflow.

That is the workflow refusing to check out a fork's code rather than a result of this change - #2188 hits the same failure. Nothing to fix on my side; the other job of that matrix stays waiting behind it. The unit tests themselves pass locally (see above).

This branch is waiting to be deployed

1 waiting deployment
unittest — d1ee596b Waiting Nov 19, 2026 by Luckyou-d via build (3.9) #1777
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant