Repository navigation
fix: detect reused pids when checking lock holders - #5505
Merged
Merged
Conversation
A lock file that outlives its holder kept Remote Control, update installs and sessions blocked whenever another process reused the recorded pid. The holder check now compares the pid's process start time against the lock's timestamp, alongside the existing boot-time and zombie checks, and Remote Control and the CLI update lock now use the same check as session locks.
🦋 Changeset detectedLatest commit: 4cc9708 The changes in this PR will be included in the next version bump. This PR includes changesets to release 6 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 |
7Sageer
marked this pull request as ready for review
November 26, 2026 08:43
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: |
sailist
approved these changes
Nov 26, 2026
5 of 6 tasks
7723qqq
pushed a commit
to 7723qqq/kimi-code
that referenced
this pull request
Dec 2, 2026
`bun scripts/upstream-ledger.mjs --refresh` at upstream/main 4eb2919 added 126 rows, so every open upstream commit and every PR it references now has a ledger row: 0/596, down from 125 at 8bf8ba3 and 126 at the fetched tip. The refresh only reads `upstream/main ^ main`, so the three release-2.6.2 commits named by the mission (MoonshotAI#4875/MoonshotAI#4905/MoonshotAI#4922) gained rows with the refresh's row shape. Settled with live probes in this tree: - ported: MoonshotAI#5533 (docs 2.8.1 changelog), MoonshotAI#4875 (registry search_url and feedback_url plus the feedback service), MoonshotAI#4905 (Kimi upload status validation in packages/core), MoonshotAI#4922 (swarm task events in sessionTaskFeed), MoonshotAI#5438 (unload only declared dynamic tools), MoonshotAI#5592 (dynamic tool replacement order), MoonshotAI#5595 (append-log known length), MoonshotAI#5573 (skill roots for every work dir), MoonshotAI#5576 (date reminder cwd comparison), MoonshotAI#5618 (session titles for skill openers), MoonshotAI#5638 (frozen loop tool baseline), MoonshotAI#5626 (legacy turn-input origins) - partial: MoonshotAI#5505 (processLiveness source and consumers present; the upstream unit test file was never carried), MoonshotAI#5571 (program-resume and OSC 7577 present; the upstream test helper was never carried), MoonshotAI#5532 (gitService half present; fsService still leaks the environment lease at fsService.ts:1133 and :1267), MoonshotAI#5557 (app-client icon registry kept; the move to app-ui was never carried) - not-applicable: MoonshotAI#4655 (upstream reverted auto review in MoonshotAI#5649), MoonshotAI#5333 (PLAYBOOK:111 deliberate flat experimental flags) M39's "likely present" list was checked, not trusted: MoonshotAI#5438 is present (upstream test passes), MoonshotAI#5487 is not (nativeStartSettleMs/leg.nativeSettled absent), MoonshotAI#5635 is not (ToolsSettled absent), MoonshotAI#5429 needs a visual probe. The remaining 194 pending rows stay open with their reasons; no feature was ported and no upstream-drift allowlist entry was added. Verification: verify -> 332 closed rows, 332 carry every file of their PR; 317 probes pass, 2 foreign-runner skips, 0 fail; a second verify is byte-identical (ledger sha256 cbf53638...). Probe tests 29 pass; bun run lint and node scripts/checks/ratchet.mjs green.
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
Resolve #5448
锁文件比持有它的进程活得更久时,只要系统把记录里的 pid 分配给了别的进程,这把锁就会被一直当作「仍被持有」:
kimi web --remote-control一直报 Remote Control 已在运行,自动更新的安装锁一直卡住,会话一直报被占用。重启之后 pid 从小号重新分配,最容易撞上;Windows 回收 pid 很快,同一次开机内也会出现(这正是 #5448 的场景,pid 被更晚启动的sshd.exe复用)。这个 PR 之后,POSIX 和 Windows 上 pid 被复用的锁都会被识别为失效并回收。Bug Reproduction Steps
kimi web --remote-control,然后强杀这个进程,~/.kimi-code/server/rc.json会留在磁盘上。rc.json里的pid指向一个正在运行、但不是原持有者的进程(POSIX 上为启动时间晚于started_at的进程,Windows 上为同一次开机内复用该 pid 的其他进程)。kimi web --remote-control:一直提示Remote Control is already running on this machine (pid …),只能手动删文件。~/.kimi-code/updates/install.lock的复现方式相同,表现是更新一直拿不到安装锁:持有者 pid 还活着时,30 分钟超时不生效。会话锁~/.kimi-code/locks/sessions/<id>.lock和会话 presence 标记在同一次开机内 pid 被复用时,表现为一直报会话被占用或 worktree 被占用。Root Cause
这几把锁判断持有者是否还活着,用的都是「这个 pid 现在有没有进程」,但 pid 还活着不代表还是原来那个进程:
rc.json)kill(pid, 0)install.lock)kill(pid, 0),30 分钟超时只在锁里没有 pid 时生效kill(pid, 0),加僵尸进程检查,加「记录时间不早于本次开机」。重启后的复用能识别,同一次开机内的复用识别不了这次是根本修复,按平台采用各自最可靠且零原生依赖的机制:
ps获取该 pid 的启动时间,如果晚于锁的写入时间,这个 pid 就一定已经换了主人。os.uptime()(GetTickCount64())不归零。改成由持有者进程提供一个只在它存活期间存在的内核命名管道(\\.\pipe\kimi-code-live-<pid>-<随机 16 字节十六进制>),把管道裸名写入锁的可选字段liveness。进程无论以何种方式退出(包括强杀或崩溃),内核都会立刻回收该管道;判活时直接检查\\.\pipe\中是否仍有同名管道,不再依赖 pid 未复用,也绕开了os.uptime()。Code Changes
改动都在已有结构里:agent-core-v2 已经有一个给会话锁和 session presence 共用的判活函数,这个 PR 把它从
persistence/backends/node-fs/上移到_base/utils/并补强,新增 Windows 进程级存活凭证currentProcessLiveness(),再让 Remote Control 和 CLI 的更新锁也改用它。判活逻辑的变化:
Windows 进程级存活凭证:
调用方的变化:
ProcessStamp.hostname和ProcessStamp.liveness均为可选字段:hostname,不传时按本机处理。liveness(仅 Windows 写入,POSIX 上不写)。旧版本都用Partial<...>按字段读取,会忽略liveness;新版本读旧锁(没有liveness)时退回kill(pid, 0)+ 开机时间比较,新旧版本可并存运行。ps报告的进程启动时间和锁里已有的写入时间比较liveness字段,判活时检查\\.\pipe\wmic在 Win11 24H2 已移除,PowerShell 冷启动耗时数百毫秒不适合同步判活;命名管道零原生依赖、同步、进程崩溃即由内核回收,实测readdirSync('\\.\pipe\')约 0.29ms,同时绕开快速启动下os.uptime()不归零的问题currentProcessLiveness()为什么同步校验server.listening而不是等'listening'事件listen()后同步检查server.listening与\\.\pipe\可见性'listening'触发前返回undefined'listening'事件在nextTick异步触发,若等该事件会导致进程里第一把锁(rc.json、install.lock及同步会话锁)必然漏写liveness;Windows 实测确认uv_pipe_bind同步创建管道,成功时server.listening立刻为true且立即可见,失败时立刻为falseprocessLiveness上移到@moonshot-ai/agent-core-v2/_base/utils/processLiveness并通过子路径引用persistence/backends/node-fs/_base/utils/与已有的_base/utils/proxy保持同层且保持零内部依赖,后续处理minidb锁时再按需要抽独立基础包ps不支持-p);误判为失效会抢走活着的持有者的锁,后果更严重阅读顺序:
packages/agent-core-v2/src/_base/utils/processLiveness.ts:跨平台判活规则与 Windows 命名管道存活凭证的全部实现。packages/remote-control/src/lock.ts、apps/kimi-code/src/cli/update/install-lock.ts、packages/agent-core-v2/src/persistence/backends/node-fs/sessionLockService.ts、packages/agent-core-v2/src/app/sessionPresence/sessionPresenceService.ts:四处锁读写透传liveness并共用processStampAlive。packages/agent-core-v2/test/_base/utils/processLiveness.test.ts是规则与 Windows 命名管道的单测;另外四个入口各有 pid 复用的回归测试。Behavior Changes and Affected Users
os.uptime(),可能识别不了liveness字段)kill(pid, 0)+ 开机时间比较unref()的本地命名管道,进程退出时由系统回收os.uptime()的取整误判为失效kill(pid, 0)ps;Windows 额外同步列举一次\\.\pipe\(实测 284 个管道平均 0.29ms)rc.json,最多 5 秒ps失败时的 stderrps的报错会打到宿主进程的 stderr受影响的人群:
kimi web --remote-control,以及 Desktop、Web 内嵌的 kap-server 的 Remote Control 开关。已知边界:
liveness字段,升级后首次遇到旧锁仍退回kill(pid, 0)+ 开机时间比较;新版本写出的锁全部带liveness。测试覆盖:
packages/agent-core-v2/test/_base/utils/processLiveness.test.ts覆盖了真实子进程(存活、已退出、启动晚于记录时间),记录早于开机,非法 pid/时间戳,僵尸进程,跨天etime格式,读不到进程信息,跨主机,以及 Windows 专属命名管道测试(跨进程强杀后模拟 pid 复用、不存在管道、绕过os.uptime()且大小写不敏感、管道目录不可读保守判活、token 稳定性)。packages/remote-control/test/remote-control.test.ts:reaps a lock whose pid now belongs to a process started after itapps/kimi-code/test/cli/update/install-lock.test.ts:sweeps an aged lock whose pid now belongs to a process started after itpackages/agent-core-v2/test/persistence/backends/node-fs/sessionLockService.test.ts:reclaims a lock whose pid now belongs to a process started after itpackages/agent-core-v2/test/app/worktree/worktreeService.test.ts:counts a session whose marker pid was reused as closed验证记录
在 macOS 上实际运行过的命令:
pnpm exec vitest run --reporter=agent packages/agent-core-v2/test/_base/utils/processLiveness.test.ts packages/agent-core-v2/test/persistence/backends/node-fs/sessionLockService.test.ts packages/agent-core-v2/test/app/worktree/worktreeService.test.ts packages/remote-control/test/remote-control.test.ts apps/kimi-code/test/cli/update/install-lock.test.ts:5 个测试文件全部通过(190 passed | 5 skipped,跳过的 5 条为 Windows 专属用例),实测验证了 macOS BSDps -o stat= -o etime= -p <pid>输出解析。pnpm exec tsc -p packages/agent-core-v2/tsconfig.json --noEmit、packages/remote-control、apps/kimi-code、packages/kap-server:全部通过。pnpm lint:全量通过(0 warnings, 0 errors)。pnpm run check:duplication 25c5e56745:通过,无新增重复。在 Windows 11(
10.0.22621x64,Nodev24.22.1)上实际运行过的验证:readdirSync('\\.\pipe\')返回裸名(287 个管道,含系统服务进程管道),连续 100 次总耗时 29.03ms(平均 0.29ms,P95 0.326ms);server.listen(pipe)返回瞬间server.listening === true且管道立即可见于\\.\pipe\,绑定冲突/非法时同步为false;pnpm exec vitest run --reporter=verbose packages/agent-core-v2/test/_base/utils/processLiveness.test.ts packages/agent-core-v2/test/persistence/backends/node-fs/sessionLockService.test.ts apps/kimi-code/test/cli/update/install-lock.test.ts:3 个文件全部通过(45 passed | 2 skipped,含全部 5 条 Windows 命名管道专项用例;2 条跳过为 POSIX 专属)。pnpm exec vitest run --reporter=verbose packages/remote-control/test/remote-control.test.ts -t "Remote Control single-instance lock":6/6 通过(整文件 52 条中,仅 2 条既有chmod 0o555用例因 Windows/管理员权限特性失败,与本改动无关)。pnpm exec vitest run --reporter=verbose packages/agent-core-v2/test/app/worktree/worktreeService.test.ts -t "sessions in the worktree":7/7 通过(含新增 presence marker pid 复用回归用例)。在 Linux 上实际运行过的命令(此前提交验证):
pnpm exec vitest run --reporter=agent跑了packages/agent-core-v2/test/persistence、sessionLifecycleService.test.ts、worktreeService.test.ts、packages/kap-server/test/remoteControl.test.ts、apps/kimi-code/test/cli/update、apps/kimi-code/test/cli/web、apps/kimi-code/test/tui/commands/web.test.ts、packages/remote-control(2 个既有的chmod 0o555用例因 root 环境失败)。未验证:
后续(不在本 PR 范围内):
ensure-daemon.lock、tower 的状态锁、minidb 的db.lock、kap-server 的实例注册表也只看 pid 判活,后续可直接复用processStampAlive和currentProcessLiveness。Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.