Closed
缺陷报告:workflow-orchestrator 单实例锁存在启动竞态 #20
KaranocaVeAdd on  Sep 28Closed at  Oct 21
KaranocaVe
KaranocaVe
Sep 28 Create

缺陷报告:workflow-orchestrator 单实例锁存在启动竞态

固定版本

仓库 cann/cannbot,源码 commit a17dd895f9497a72ce57ba221e0ba01959c14923。基线 harness/workflow-orchestrator/scripts/orchestrator.py 与隔离测试副本的 SHA256 均为 d28d49269b96f3ec7c75a4aed8a3d6daf45cc13bb94f2a9f8477f60a4908ff9a。

现象

acquire_lock(work_dir) 先检查锁文件是否存在,再读取 PID 并以普通写模式创建或覆盖文件。两个 orchestrator 若同时启动并在锁文件创建前检查,都能越过检查并返回成功,单实例约束失效。

确定性基线探针让两个并发调用在检查锁文件后同步继续,结果为 lock_checks=[False, False] concurrent_acquisitions=2。完整命令和退出码见 validation.md。

影响

同一工作目录可同时运行多个 orchestrator;它们可能重复派发任务并并发更新工作流状态。已直接确认的是两次锁获取都成功;没有声称已测得任务重复或状态丢失。

根因与修复

检查文件与写入文件不是互斥操作,也没有使用原子创建,因此不能用来仲裁竞争。补丁对稳定的锁文件 inode 使用非阻塞独占 flock,只有取得内核锁的进程才写入 PID。持有文件句柄直到进程退出;竞争失败或写入异常时关闭句柄,并且不删除锁文件,避免其他进程锁住不同 inode。

修复范围限于 harness/workflow-orchestrator/scripts/orchestrator.py,并新增同步启动回归测试。

Validation

验证记录

固定源码与环境

  • 仓库:cann/cannbot
  • 源码 commit:a17dd895f9497a72ce57ba221e0ba01959c14923
  • Python:3.11.15
  • 基线脚本 SHA256:d28d49269b96f3ec7c75a4aed8a3d6daf45cc13bb94f2a9f8477f60a4908ff9a;与固定源码中的脚本一致。
  • 本问题位于 Python 工作流编排器,无 NPU 执行路径。

基线竞态复现

在固定源码仓库根目录执行。探针在两个调用都读取锁文件状态后放行,因此稳定复现“双方都观察到锁不存在”的竞态边界:

PYTHONDONTWRITEBYTECODE=1 python3 - <<'PY'
import sys
import tempfile
import threading
from concurrent.futures import ThreadPoolExecutor

sys.path.insert(0, "harness/workflow-orchestrator/scripts")
import orchestrator

barrier = threading.Barrier(2)
original_exists = orchestrator.os.path.exists
observed = []

def synchronized_exists(path):
    result = original_exists(path)
    if path.endswith("orchestrator.lock"):
        observed.append(result)
        barrier.wait(timeout=5)
    return result

orchestrator.os.path.exists = synchronized_exists
try:
    with tempfile.TemporaryDirectory(prefix="wo-baseline-") as work_dir:
        with ThreadPoolExecutor(max_workers=2) as pool:
            owners = list(pool.map(lambda _: orchestrator.acquire_lock(work_dir), range(2)))
        print("lock_checks=%s concurrent_acquisitions=%d" %
              (observed, sum(owner is not None for owner in owners)))
        assert sorted(observed) == [False, False]
        assert all(owner is not None for owner in owners)
finally:
    orchestrator.os.path.exists = original_exists
PY
  • 退出码:0
  • 实际输出:lock_checks=[False, False] concurrent_acquisitions=2
  • 预期:最多一个调用成功;基线中两个调用均成功。

修复验证

在应用补丁的隔离 workflow-orchestrator 副本中运行:

  • PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s harness/workflow-orchestrator/tests -p 'test_orchestrator_lock.py' -v
    • 退出码 0;新增并发锁测试 1 项通过。
    • 两个调用在实际 flock 前同步,断言只有一个持有锁;释放后再次获取成功,锁文件保持存在。
  • PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s harness/workflow-orchestrator/tests -v
    • 退出码 0;155 项通过,用时 43.417 秒。
  • uvx --from ruff==0.14.14 ruff check --output-format github --ignore E501,RUF001,RUF002,RUF003,E402,E701,E702,N815 harness/workflow-orchestrator/scripts/orchestrator.py harness/workflow-orchestrator/tests/test_orchestrator_lock.py
    • 退出码 0;使用目标仓库 pre-commit 固定的 Ruff 0.14.14。
  • git apply --unidiff-zero --check --whitespace=error <fix.patch>
    • 在固定源码 checkout 上退出码 0,补丁可应用且新增源码无尾随空白;补丁使用零上下文 hunks。
  • git diff --cached --check
    • 在隔离基线副本检查两项实际代码变更,退出码 0。
  • 首轮 Ruff 0.14.14 格式检查在未格式化脚本上返回退出码 1;目标仓库 .pre-commit-config.yaml 固定使用 Ruff 0.14.14 ruff-format。为使 PR 中被修改的 Python 文件符合该检查,对 orchestrator.py 做了全文件格式化(包含锁修复之外的既存格式差异,仅排版变化)。格式化后的检查结果:
    • uvx --from ruff==0.14.14 ruff format --check harness/workflow-orchestrator/scripts/orchestrator.py:退出码 0。
    • uvx --from ruff==0.14.14 ruff check --output-format github --ignore E501,RUF001,RUF002,RUF003,E402,E701,E702,N815 harness/workflow-orchestrator/scripts/orchestrator.py harness/workflow-orchestrator/tests/test_orchestrator_lock.py:退出码 0。
    • PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s harness/workflow-orchestrator/tests -v:退出码 0,155 项通过;在格式化后运行。
    • git diff --check:退出码 0。

结论与边界

基线竞态和修复后的单锁归属均有可重复验证,目标目录全量单元测试通过。没有进行真实 CLI 多进程端到端任务执行;下游任务重复派发和状态竞争是由单实例锁失效推得的影响,未单独量化。

关联 PR: https://gitcode.com/cann/cannbot/merge_requests/58

likedislike
KaranocaVeKaranocaVe
Sep 28 Associated pull request
KaranocaVeKaranocaVe
Sep 28 Modify description of issue
KaranocaVeKaranocaVe
Sep 28 Modify description of issue
KaranocaVe
KaranocaVe
Oct 21 Comments:

/close

likedislike
CANN-robotCANN-robotCollaborators
Oct 21 changed custom state from 进行中 to 已完成
CANN-robotCANN-robotCollaborators
Oct 21 Close issue
CANN-robotCANN-robotCollaborators
Oct 21 Added label: resolved
KaranocaVe
KaranocaVe
Oct 22 Comments:

@jon_jin @xutianze 您好,这个 Issue 对应的修复已经合入:https://gitcode.com/cann/cannbot/merge_requests/58

开发者计划反馈,Issue 需要有 Accepted 标签才能计入积分,仅关闭或标记 resolved 还不够。目前这条 Issue 尚未标记 Accepted,麻烦您结合问题和已合入的修复复核一下;确认符合受理条件后,帮忙补上 Accepted 标签。

感谢您的支持!

likedislike
jon_jinjon_jinCollaborators
11 days ago Assign zhou-qilong as responsible person
jon_jin
jon_jinCollaborators
11 days ago Comments:

@zhou-qilong 你好,本 issue 涉及 harness,按 CANNBot SIG 最新责任田配置补指派给你。本次仅补齐已关闭 issue 的历史责任归属,不改动原关闭状态或结论。

— jon_jin

likedislike