fix(agent): report background task failures

This commit is contained in:
yu-xin-c authored and Xubin Ren committed 2026-09-24 19:55:21 +08:00
1 parent eb3fc07087
commit aa117e7271
2 files changed
+86 -1

No files matched your search

+12 -1
View File
@@ -1681,11 +1681,22 @@ class AgentLoop:
if errors:
raise BaseExceptionGroup("failed to close agent resources", errors)
def _background_task_done(self, task: asyncio.Task[Any]) -> None:
self._background_tasks.discard(task)
if task.cancelled():
return
exception = task.exception()
if exception is not None:
logger.opt(exception=exception).error(
"Background task '{}' failed",
task.get_name(),
)
def schedule_background(self, coro: Coroutine[Any, Any, Any]) -> None:
"""Schedule a coroutine as a tracked background task (drained on shutdown)."""
task = asyncio.create_task(coro)
self._background_tasks.add(task)
task.add_done_callback(self._background_tasks.discard)
task.add_done_callback(self._background_task_done)
def stop(self) -> None:
"""Stop the agent loop."""