diff --git a/astrbot/core/utils/session_waiter.py b/astrbot/core/utils/session_waiter.py index f196dd00a2..be4944a2af 100644 --- a/astrbot/core/utils/session_waiter.py +++ b/astrbot/core/utils/session_waiter.py @@ -31,6 +31,8 @@ def __init__(self) -> None: def stop(self, error: Exception | None = None) -> None: """立即结束这个会话""" + if self.current_event and not self.current_event.is_set(): + self.current_event.set() if not self.future.done(): if error: self.future.set_exception(error) @@ -46,6 +48,9 @@ def keep(self, timeout: float = 0, reset_timeout=False) -> None: 当 reset_timeout 设置为 False 时, 代表继续维持原来的超时时间, 新 timeout = 原来剩余的timeout + timeout (可以 < 0) """ + if self.future.done(): + return + new_ts = time.time() if reset_timeout: diff --git a/changelogs/unreleased.md b/changelogs/unreleased.md index ca797ace82..7d59c95e0c 100644 --- a/changelogs/unreleased.md +++ b/changelogs/unreleased.md @@ -2,6 +2,8 @@ - QQ Official: failed image uploads no longer log base64 payloads, avoiding dashboard freezes from oversized log entries. Retry attempts decrease from five to three, with one summary warning. +- Session waiters now wake their timeout task immediately when completed early, avoiding stale asyncio tasks for the remainder of the configured timeout. + - 「插件 → 管理行为 → 函数工具」现在只列出插件工具。Agent 运行时工具由配置文件的 AI 功能设置控制,MCP 工具可在「插件 → MCP」查看。 - Extensions → Handlers → Function Tools now lists plugin tools. Configure Agent runtime tools in the profile's AI capability settings and view MCP tools under Extensions → MCP Servers. diff --git a/tests/unit/test_session_waiter_pipeline.py b/tests/unit/test_session_waiter_pipeline.py index ca59a5b899..8d7b2c96a1 100644 --- a/tests/unit/test_session_waiter_pipeline.py +++ b/tests/unit/test_session_waiter_pipeline.py @@ -1,5 +1,6 @@ """Session waiters are dispatched by the core pipeline after access checks.""" +import asyncio import copy from types import SimpleNamespace from unittest.mock import AsyncMock @@ -19,11 +20,47 @@ from astrbot.core.utils.session_waiter import ( USER_SESSIONS, DefaultSessionFilter, + SessionController, SessionFilter, SessionWaiter, ) +@pytest.mark.asyncio +async def test_stopping_session_waiter_wakes_timeout_task(): + controller = SessionController() + holding_started = asyncio.Event() + holding_finished = asyncio.Event() + original_holding = controller._holding + + async def track_holding(event, timeout): + holding_started.set() + try: + await original_holding(event, timeout) + finally: + holding_finished.set() + + controller._holding = track_holding + controller.keep(timeout=60, reset_timeout=True) + await holding_started.wait() + controller.stop() + + await asyncio.wait_for(holding_finished.wait(), timeout=1) + assert controller.future.done() + + +@pytest.mark.asyncio +async def test_keep_after_stop_does_not_start_timeout_task(): + controller = SessionController() + controller.stop() + + try: + controller.keep(timeout=60, reset_timeout=True) + assert controller.current_event is None + finally: + controller.stop() + + @pytest.mark.asyncio @pytest.mark.parametrize( ("private", "state"),