Repository navigation
fix(agent-core-v2): check the Tower lock holder before taking the git reaper guard - #5479
Merged
Merged
Conversation
… reaper guard Every Tower lock waiter runs a stale-lock check every 250 ms. The check took the git-backed reaper guard first (rev-parse, hash-object, update-ref, then update-ref -d on release) and only then read the lock file to see whether its holder had died. With a live holder all of that git work is wasted, and with many waiters the spawned git processes starve the holder itself: under load one concurrent send/register case spent 175 s of cumulative time in reaper checks within 11 s and timed out on the 10 s lock wait in 12 of 12 runs. Read the lock file and check the holder's liveness before taking the guard, and skip the round while the holder is alive. The guarded path is unchanged: it still re-reads the lock, confirms the holder is dead twice and compares the content before removing it. The two reaper race cases paused on the first liveness check, which used to run under the guard; they now pause explicitly once the first reaper holds the guard, and the first one counts guard acquisitions so it proves the second reaper was locked out. New cases pin that a live holder never makes a waiter take the guard, and that the guarded recheck still protects a lock replaced between the unguarded check and the guard.
🦋 Changeset detectedLatest commit: 6b50a20 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
commit: |
4 of 6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Requirement or Bug
Tower 的多个 agent 同时发消息、注册或更新任务时,在负载较高的机器上,工具调用会报
timed out after 10000ms waiting for the tower state lock或... inbox publication lock。agent-core-v2 全量测试里features/tower/store.test.ts的三条并发用例也因此偶发失败。这个 PR 之后,持锁者还活着时,等锁的一方不再起 git 进程,同样的负载下不再超时。Bug Reproduction Steps
pnpm exec vitest run packages/agent-core-v2/test/features/tower/store.test.ts -t "sends without waiting for the state lock and preserves",另开 6 个吃满 CPU 的进程。TowerProtocolError: timed out after 10000ms waiting for the tower state lock或inbox publication lock。同样的用例空闲时 0.9 秒通过。Root Cause
等锁的一方每 250ms 检查一次锁是否已被死掉的进程遗留(
reapStaleStateLock)。这个检查先拿一个由 git ref 实现的回收守卫,过程中要起rev-parse、hash-object -w、update-ref几个 git 进程,释放时还要起一次update-ref -d。拿到守卫之后,它才去读锁文件,用process.kill(pid, 0)判断持锁者是否已死。持锁者活着时,这些 git 调用全部白费。等锁的人一多,起出来的 git 进程反过来把持锁者也拖慢了,形成自我放大。在上面那条用例里临时加计时:
用例里的持锁者一直是活着的本进程,所以这些回收检查没有一次真正需要回收。这是根本修复:去掉了持锁者活着时的无用工作,没有调整超时。
Code Changes
reapStaleStateLock先读锁文件、判断持锁者是否已死,活着就跳过这一轮,不碰 git。只有看起来已死,才走原来的守卫路径readDeadStateLockHolder,守卫外的预检和守卫内的两次确认共用。守卫内仍然要两次确认已死、两次读到的内容一致,才删锁关键决策
kill(pid, 0),判定和守卫内完全相同为什么预检不会误删锁
预检只会让回收检查提前返回 false,永远不会让它删掉什么:删锁仍然只发生在守卫内,条件和原来一样。
预检的判定和守卫内第一次确认相同,所以凡是预检跳过的轮次,在原来的代码里拿到守卫后也会返回 false。唯一的差别在时序:持锁者在预检之后、下一轮检查之前死掉的话,回收会晚一轮(250ms)。
反过来的情况也有覆盖:一个回收者通过了预检(锁看起来是死进程留下的),但在它拿到守卫之前,锁已被别人清掉、又被新的持锁者拿到。这时守卫内的复查读到的是新持锁者,所以返回 false。新增的一条用例固定了这个顺序:删掉守卫内的复查,它就会失败。
测试
waits for a live holder in %s without taking the reaper guard(新增,两种情况)rechecks the holder under the guard when the lock is replaced before the guard is taken(新增)never deletes a replacement created after stale removal、uses one reaper guard across real and symlink checkout aliases(调整)stateLockHolderIsDead调用处暂停,因为旧代码里第一次存活检查发生在守卫内。加了预检后,第一次存活检查提前到守卫外,暂停时还没拿到守卫,两个回收者的角色就会对调:真正回收死锁的变成了第二个,没有误删。现在改成在第一个回收者拿到守卫之后显式暂停,场景和断言不变,在新旧代码上都通过。第一条原来的staleChecks只会是 1,toBe(1)其实什么也没检查。现在改成统计守卫被拿到的次数,能真正证明第二个回收者被守卫挡住了Behavior Changes and Affected Users
受影响的人群:打开了实验特性 Tower 的用户,开关是
KIMI_CODE_EXPERIMENTAL_TOWER或config.toml的[experimental] tower,CLI 和桌面端都有。没有改动提示词、配置项或契约文件。refs/tower/state-lock-reaper-*)recovers from incomplete legacy claims and a crashed reaper guard继续覆盖这条路径changeset:
@moonshot-ai/agent-core-v2技术条目,CLI 和桌面端各一条(patch)。验证
只在 macOS 上验证过,Linux 和 Windows 交给 CI。改动只涉及读文件和
process.kill(pid, 0),原来的守卫路径就已经在各平台上这样调用。新增用例用到的process.ppid,在 Windows 上同样可以用kill(pid, 0)探测。acquireStateLockReaper被调用了 1 次);复查用例在删掉守卫内的复查后失败。两条调整过的竞态用例,在新旧产品代码上都通过。packages/agent-core-v2/test/features/tower下 598 条通过。packages/node-sdk/test/sdk-rpc-client-v2.test.ts用到了TowerStore,173 条通过。pnpm lint通过,agent-core-v2 类型检查通过,pnpm run check:duplication没有新增重复。Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.