From 10c79fb43b6247e8fe731cc85d4c685f1c79122f Mon Sep 17 00:00:00 2001 From: icywwwaa <3223850276@qq.com> Date: Tue, 24 Nov 2026 10:05:39 +0800 Subject: [PATCH 1/2] fix: wake session waiter timeout task on stop - Signal the active timeout wait when a session ends early. - Add a regression test and changelog entry. --- astrbot/core/utils/session_waiter.py | 2 ++ changelogs/unreleased.md | 2 ++ tests/unit/test_session_waiter_pipeline.py | 25 ++++++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/astrbot/core/utils/session_waiter.py b/astrbot/core/utils/session_waiter.py index f196dd00a24..455e1416e91 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) diff --git a/changelogs/unreleased.md b/changelogs/unreleased.md index ca797ace82c..7d59c95e0c2 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 ca59a5b8999..1c6a8f75de2 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,35 @@ 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 @pytest.mark.parametrize( ("private", "state"), From 6b6adf19e478b0ee87d73936b4b413fa2b1e17e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=B0=B8=E8=B5=AB?= <1259085392@qq.com> Date: Tue, 24 Nov 2026 16:40:09 +0900 Subject: [PATCH 2/2] fix: ignore keep on stopped sessions --- astrbot/core/utils/session_waiter.py | 3 +++ tests/unit/test_session_waiter_pipeline.py | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/astrbot/core/utils/session_waiter.py b/astrbot/core/utils/session_waiter.py index 455e1416e91..be4944a2afd 100644 --- a/astrbot/core/utils/session_waiter.py +++ b/astrbot/core/utils/session_waiter.py @@ -48,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/tests/unit/test_session_waiter_pipeline.py b/tests/unit/test_session_waiter_pipeline.py index 1c6a8f75de2..8d7b2c96a16 100644 --- a/tests/unit/test_session_waiter_pipeline.py +++ b/tests/unit/test_session_waiter_pipeline.py @@ -49,6 +49,18 @@ async def track_holding(event, timeout): 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"),