Skip to content

test: wait for the tower lock holder instead of polling the lock file - #5245

Merged
RealKai42 merged 1 commit into
mainfrom
fix/tower-stale-release-lock-flake
Nov 23, 2026
Merged

RealKai42 merged 1 commit into
mainfrom
fix/tower-stale-release-lock-flake

Conversation

@RealKai42

Copy link
Copy Markdown
Collaborator

Requirement or Bug

跑 agent-core-v2 全量测试时,packages/agent-core-v2/test/features/tower/store.test.ts 里的 concurrent state writes > a stale release cannot delete another holder's state lock 偶发失败,报 expected '' to match /pid=\d+ since=\S+ token=[0-9a-f-]+/;单独跑这个文件时稳定通过。这个 PR 修掉用例的同步方式,让它每次都确定性地通过,并且每次运行都覆盖锁文件「已创建、内容还没写」的那一刻。没有对应的 issue。

Bug Reproduction Steps

不施加 CPU 压力时很难碰到,下面是确定性复现的办法(不依赖 sleep、重试或超时):

  1. 在原用例里,用 FileHandle.prototype.writeFile 的 spy 把新持有者写入锁内容(pid=… since=… token=…)的那一步挂起。这时 open(lockPath, 'wx') 已经建好了锁文件,但文件是空的。
  2. 运行原来的轮询循环:锁文件一出现它就读,读到的是 ''。
  3. 原断言失败,报错和全量运行时完全一致。连续运行 5 次,5 次都失败。

Root Cause

TowerStore.tryAcquireStateLock 分两步拿锁:先 open(lockPath, 'wx') 建出空文件,再 handle.writeFile 写入持有者信息,两步之间至少隔一次 libuv 线程池往返。原用例每 10ms 轮询一次,readFile 一成功就退出,把「锁文件存在」当成了「second 已经持有锁」。全量运行时 CPU 争用激烈,readFile 的 open、fstat、read 可能正好落在这两步之间,读到空字符串。

sequenceDiagram
    participant T as 用例
    participant S as second(新持有者)
    participant F as 锁文件
    S->>F: open(lockPath, 'wx'),文件存在但内容为空
    T->>F: readFile 成功,读到 ''
    Note over T: 退出轮询,断言失败
    S->>F: writeFile("pid=… since=… token=…")
    S->>S: 进入临界区
Loading

这是用例同步方式的问题,这次是根本修复:锁真正拿到的信号是持有者进入临界区,而不是锁文件出现。用例改成等这个信号。

生产代码在空内容窗口里的行为(已实测,本 PR 不改)
读锁文件的地方 读到空内容时 结论
releaseStateLock(过期的 release) 内容里没有自己的 token,不删除 安全
reapStaleStateLock 解析不出 pid,返回 false,不回收 安全,不会抢走正在拿锁的人的锁
withProtocolLock 超时报错 held by 后面为空,并说成另一个 tower 进程 只是报错文字不准,要正好在窗口里超时才会出现

另外,如果进程恰好死在 open 和 writeFile 之间,会留下一个空锁文件,且不会被自动回收。之后的调用会明确报超时错误,需要手动删锁,不会悄悄失败。这种情况概率极低,和这次的偶发失败无关,所以没有放进本 PR。

Code Changes

只改了一个用例。改动后的流程:

 hold staleDuringCreate, wait until it enters the lock
 impatient store times out on the held lock
 remove the lock file
-start second
-poll readFile every 10ms until the lock file exists
-assert the content is a full holder line
-release first (stale), assert the lock is unchanged
-release second, assert the lock is removed
+hold staleAfterWrite, wait until it enters the lock
+remove the lock file
+spy on writeFile: when the new holder writes "pid=…"
+  release staleDuringCreate (stale) inside the create-to-write window
+  record the lock content, which must be '' (file still there)
+hold holder, wait until it enters the lock, restore the spy
+assert the content is a full holder line
+release staleAfterWrite (stale), assert the lock is unchanged
+release holder, assert the lock is removed

每个持锁方都在临界区里发 entered 信号,用例等到信号才读锁文件,原来的 setTimeout 轮询删掉了。spy 让每次运行都把「锁文件已建、内容未写」的窗口撑开,并在窗口里做一次过期 release,所以旧的同步方式在这个用例下每次都会失败,这个用例本身就是回归测试。staleAfterWrite 在内容写完后再做一次过期 release,保住了原用例覆盖的情形。用 FileHandle.prototype.writeFile spy 的写法沿用了同文件里 inbox 原子发布那个用例。

决策 选择 没选 理由
修在哪里 只改用例的同步方式 改生产代码,用临时文件加 link 让锁内容原子写入 生产代码里读锁的地方在空内容窗口里都是安全的;原子写会让每次状态写入多几次系统调用,崩溃会留下临时文件要清理,不支持硬链接、退回 wx 时窗口依然存在,而且改的是并发锁本身

Behavior Changes and Affected Users

None。只改了 packages/agent-core-v2/test/features/tower/store.test.ts 里的这一个用例,没有改生产代码、默认值或契约文件。

受影响模块:tower 状态锁的测试。覆盖情况:过期 release 在两种时机下都不能删除别人的锁,一次在新持有者建锁和写内容之间,一次在内容写完之后。

验证结论:本机 macOS 上,这个用例单独运行 20 次全部通过。临时把 releaseStateLock 改坏成三种写法(读到空内容就删、不比对 token、只在内容非空时删),这个用例都会失败。整个测试文件通过,lint 和类型检查都是 0 退出。Linux 由 CI 验证,Windows 没有验证。

实际运行的命令和结果
  • pnpm exec vitest run --reporter=agent packages/agent-core-v2/test/features/tower/store.test.ts -t "a stale release cannot delete":连续 20 次,20 次通过。
  • 上面三种 releaseStateLock 改坏写法,各跑一次,都失败,跑完已还原源码。
  • pnpm exec vitest run --reporter=agent packages/agent-core-v2/test/features/tower/store.test.ts:229/229 通过。
  • pnpm lint:退出码 0。
  • pnpm exec tsc -p packages/agent-core-v2/tsconfig.json --noEmit:退出码 0,这个 tsconfig 包含 test 目录。
  • pnpm run check:duplication <base-sha>:没有新增重复。
  • pnpm exec vitest run --reporter=agent packages/agent-core-v2:store.test.ts 全部通过。test/tool/tool.test.ts 里有 3 个用例 30s 超时(treats timeoutMs=0 as no deadline for background subagents and preserves the metadata、runs foreground subagents without a deadline under print mode defaults、runs background subagents without a deadline under print mode defaults),单独跑这个文件也每次失败。本 PR 相对 main 只改了 store.test.ts,所以这 3 个失败与本 PR 无关。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

The stale-release case polled the state lock file and read it as soon as
it existed. tryAcquireStateLock creates the file with open('wx') and only
then writes the holder line, so under load the read could land between
the two steps and see an empty file.

Each holder now signals when it enters its critical section, and the test
reads the lock only after that signal. The test also holds the new
holder's create-to-write window open with a writeFile spy and runs one
stale release inside it, so the empty-content case is covered on every
run. A second stale holder releases after the content is written, which
keeps the case the test covered before.
@changeset-bot

changeset-bot Bot commented Nov 23, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: eff4dc8

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-23T03:39:20.180392Z eff4dc8 PR opened
🔒 Security Review ✅ Completed 2026-11-23T03:41:16.209016Z eff4dc8 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Nov 23, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@eff4dc8
npx https://pkg.pr.new/@moonshot-ai/kimi-code@eff4dc8

commit: eff4dc8

@RealKai42
RealKai42 merged commit 5a80757 into main Nov 23, 2026
35 of 37 checks passed
@RealKai42
RealKai42 deleted the fix/tower-stale-release-lock-flake branch November 23, 2026 04:44
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 25, 2026
Rewrite the stale-release case to signal holder entry and hold the
create-to-write window with a writeFile spy, so a stale release during
lock creation observes empty content deterministically and the post-write
case keeps the covered content (MoonshotAI#5245). jest.spyOn replaces vitest's vi and
FileHandle is imported. Ledger: 5038 not-applicable (its case is absent
from the fork's tower suite) and 5080 equivalent.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant