Skip to content

[Bug] cron active_agent 任务用 future_task 删除自身时死锁,行永久停在 status='running' #11063

Closed

Description

@chuji2025

问题描述

一个 active_agent 类型的 cron 任务在运行中调用 future_task 工具删除自己正在运行的任务时,协程死锁:日志停在「使用工具:future_task,参数:{'action': 'delete', 'job_id': '...'}」后再无任何输出,cron_jobs 表对应行的 status 永久停在 running、last_run_at/next_run_time 不再更新,该任务从此不再被调度。

事件循环本身没有死(容器 healthy、dashboard 正常、其他任务正常触发),只是这条 cron 协程悬停在 delete_job 里。

复现步骤

  1. 新建一个 cron 任务,类型选 active_agent,note 末尾加上「当用户回复确认后,调用 future_task 删除本任务」之类的指令。
  2. 等它触发(或手动触发)。
  3. agent 按 note 调用 future_task 删自己。
  4. ✅ 预期:任务被删除,或至少 _run_job 的 finally 把 status 写回 completed。
  5. ❌ 实际:日志卡在「使用工具:future_task」,行 status='running' 永久不更新。

AstrBot 版本

4.29.4(Docker 镜像 sha256:72f341f0ffcf6621ef820277642c5d63955f7beeac3683433ce09cac96379c78,image created 2026-10-31)

操作系统

Linux(容器内)

部署方式

Docker(docker-compose)

你使用的协议类型

NapCat(OneBot v11,aiocqhttp 适配器)

根因分析

死锁来自 asyncio.current_task() 在「工具生成器被放进独立 task 推进」之后不再等于 _run_job 的外层 task,导致 delete_job 的自排除逻辑失效。

调用链

  1. core/cron/manager.py:379 _run_job 入口:

    task = asyncio.current_task()
    ...
    self._running_tasks.setdefault(job_id, set()).add(task)

    登记的是 _run_job 这个外层 task。

  2. agent 运行时,工具执行器走 core/agent/runners/tool_loop_agent_runner.py:1615:

    executor = iterate_in_task(executor)

    而 core/utils/async_generator.py:58:

    owner = asyncio.create_task(produce())

    即工具生成器在子 task 里 await anext(generator)。

  3. agent 调用 future_task → core/tools/cron_tools.py:333 → manager.delete_job(job_id)。

  4. core/cron/manager.py:231-258 delete_job:

    tasks = [
        task for task in self._running_tasks.get(job_id, set())
        if task is not asyncio.current_task() and not task.done()
    ]
    for task in tasks:
        task.cancel()
    await asyncio.gather(*tasks, return_exceptions=True)
    await self.db.delete_cron_job(job_id)

    此时 asyncio.current_task() 是 iterate_in_task 建的子 task(甚至 tool_loop_agent_runner.py:1622 又 asyncio.create_task 套了一层),不是 _running_tasks 里登记的 _run_job 外层 task → 排除条件失效 → 它 cancel 并 await gather 等包含自己的上游 task → 自锁。

  5. 因为卡在 delete_job 里,_run_job 的 finally(写回 status/last_run_at/next_run_time)永远不执行 → 行永久 status='running'。

引入版本

iterate_in_task(astrbot/core/utils/async_generator.py)由 PR #10474 fix: keep async tool generators in one task(作者 zouyonghe,2026-10-09 合并,merge_commit 33ef43be3fd2f99c5a11ce803402376cead25b0a)新增,并在 tool_loop_agent_runner.py:1615 接入。该文件在 v4.29.0-beta.1 中不存在(404),v4.29.0-beta.2 起出现,内容与 v4.29.4 完全相同。

因此这是 v4.29.0 引入的回归。 v4.28.x 及更早工具链直接在调用方 task 里 anext(executor),delete_job 里 asyncio.current_task() 就是 _run_job 的 task,排除逻辑有效,所以 4.28 用着没事。

delete_job 的 docstring 明确写 "Runs that have already started are cancelled and drained",设计意图就是允许任务删自己,所以这是排除条件在新执行模型下失效,不是设计不支持。

修复建议

delete_job 的排除条件不能再用 asyncio.current_task() 直接比对。可选:

  • 方案 A(最小):_run_job 入口把自身 task 通过 contextvars.ContextVar 透传到工具执行链,delete_job 用该 ContextVar 比对。
  • 方案 B:delete_job 不 cancel 自己,而是设一个「正在删除」标记,让 _run_job 的 finally 感知到并跳过 delete_run_once 自删(避免重复删),由 delete_job 直接落库删除。
  • 方案 C:future_task 工具在「删除的 job_id == 当前运行的 job_id」时直接 raise / 标记,由 _run_job 收尾时处理,不让 agent 协程里直接 await delete_job。

临时规避(已在我的环境验证有效)

把该 cron 任务的 note 末尾加上:「不要调用 future_task 或其他工具删除/修改本任务,只发消息即可。」 之后该任务每小时触发均正常,_run_job finally 正常写回。

截图/日志

关键日志(容器 stdout,12:00 那次触发):

[12:00:03.316] ... 使用工具:future_task,参数:{'action': 'delete', 'job_id': '756ae0cc-dc32-442a-b553-9a6a8c1114f3'}
(此后该任务再无任何日志)

cron_jobs 表对应行:

job_id    = 756ae0cc-dc32-442a-b553-9a6a8c1114f3
status    = running   ← 永久停在这
last_run_at = 2026-11-09 04:00:00 UTC
next_run_time 不再更新

容器状态正常:Up (healthy),dashboard curl :6185 返回 200。

Activity

  1. moved this from Todo to In Progress in AstrBot Roadmapon Nov 10, 2026
  2. Soulter commented on Nov 10, 2026

    @Soulter
    Member

    您好,最新master已经修复这个问题。请等待下一个版本

  3. moved this from In Progress to Done in AstrBot Roadmapon Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions