From cc9cb0b49853e3085a0fa3a0616c2bcb0a727da1 Mon Sep 17 00:00:00 2001 From: lhjl-j <19893241978@163.com> Date: Tue, 10 Nov 2026 12:44:42 +0800 Subject: [PATCH 1/2] fix: exclude the owning cron run when a tool deletes that job --- astrbot/core/cron/manager.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/astrbot/core/cron/manager.py b/astrbot/core/cron/manager.py index 904cd87c14c..8004dec78a1 100644 --- a/astrbot/core/cron/manager.py +++ b/astrbot/core/cron/manager.py @@ -2,6 +2,7 @@ import json import re from collections.abc import Awaitable, Callable +from contextvars import ContextVar from datetime import datetime, timezone from typing import TYPE_CHECKING, Any from zoneinfo import ZoneInfo @@ -21,6 +22,11 @@ _CRONTAB_WEEKDAY_NAMES = ("sun", "mon", "tue", "wed", "thu", "fri", "sat") _CRONTAB_WEEKDAY_PATTERN = re.compile(r"^(?:(\*)|(\d+)(?:-(\d+))?)(?:/(\d+))?$") +# The task that owns the currently executing cron run. Tool generators are +# advanced in child tasks (astrbot.core.utils.async_generator.iterate_in_task), +# so asyncio.current_task() inside a tool is not the task registered by _run_job. +_ACTIVE_JOB_TASK = ContextVar("active_cron_job_task", default=None) + PROACTIVE_AGENT_CRON_WOKE_USER_PROMPT = ( "\n" @@ -245,10 +251,14 @@ async def delete_job(self, job_id: str) -> None: try: self._remove_scheduled(job_id) self._basic_handlers.pop(job_id, None) + # A tool runs in a child task; cancelling the run we are inside + # and awaiting it here deadlocks, so exclude its owning task too. + owner = _ACTIVE_JOB_TASK.get() + current = asyncio.current_task() tasks = [ task for task in self._running_tasks.get(job_id, set()) - if task is not asyncio.current_task() and not task.done() + if task is not current and task is not owner and not task.done() ] for task in tasks: task.cancel() @@ -386,6 +396,7 @@ async def _run_job( # Register before the first await so delete_job always snapshots a run # that has passed the deletion-marker check. self._running_tasks.setdefault(job_id, set()).add(task) + owner_token = _ACTIVE_JOB_TASK.set(task) try: job = await self.db.get_cron_job(job_id) if not job or (not job.enabled and not ignore_enabled): @@ -424,6 +435,7 @@ async def _run_job( # One-shot runs remove themselves, excluding their own task. await self.delete_job(job_id) finally: + _ACTIVE_JOB_TASK.reset(owner_token) running = self._running_tasks.get(job_id) if running is not None: running.discard(task) From fe9e4e0861432c0252fc090b8988563bf8d0b408 Mon Sep 17 00:00:00 2001 From: lhjl-j <19893241978@163.com> Date: Tue, 10 Nov 2026 18:11:00 +0800 Subject: [PATCH 2/2] fix: annotate cron owner ContextVar and clarify delete_job contract Address the type-annotation and docstring findings from the Sourcery review on PR #11071. No runtime behaviour changes. - Annotate _ACTIVE_JOB_TASK as ContextVar[asyncio.Task | None] so a basic-mode type checker accepts _ACTIVE_JOB_TASK.set(task). - Clarify that delete_job() excludes the run owning the current call chain, which may still be executing when the method returns. --- astrbot/core/cron/manager.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/astrbot/core/cron/manager.py b/astrbot/core/cron/manager.py index 8004dec78a1..0b43da24953 100644 --- a/astrbot/core/cron/manager.py +++ b/astrbot/core/cron/manager.py @@ -25,7 +25,9 @@ # The task that owns the currently executing cron run. Tool generators are # advanced in child tasks (astrbot.core.utils.async_generator.iterate_in_task), # so asyncio.current_task() inside a tool is not the task registered by _run_job. -_ACTIVE_JOB_TASK = ContextVar("active_cron_job_task", default=None) +_ACTIVE_JOB_TASK: ContextVar[asyncio.Task | None] = ContextVar( + "active_cron_job_task", default=None +) PROACTIVE_AGENT_CRON_WOKE_USER_PROMPT = ( @@ -241,8 +243,11 @@ async def update_job(self, job_id: str, **kwargs) -> CronJob | None: async def delete_job(self, job_id: str) -> None: """Remove a schedule and drain its active runs before deleting its row. - Runs that have already started are cancelled and drained. Runs that - start after deletion begins return without executing. + Runs that have already started are cancelled and drained, except the + run that owns the current call chain: that run is excluded so a tool + deleting its own job does not cancel and await itself, and it may + still be executing when this method returns. Runs that start after + deletion begins return without executing. Args: job_id: Job to delete, including any currently executing run.