Repository navigation
feat(node): prevent same-host duplicate node start with agent-node.lock (Layer 1) - #3879
Conversation
…ock (Layer 1)
- Add agent-node/src/node-instance-lock.ts: atomic O_CREAT|O_EXCL|O_NOFOLLOW per-node lock (<nodeDir>/agent-node.lock) storing {pid, alias, node_id, hostname, started_at, token} while holding the fd open for the lifetime of the process
- On EEXIST, inspect existing lock (refusing symlinks) and check holder PID liveness:
- Alive -> exit cleanly with plain-Chinese message naming the running PID and pointing to `anet node stop`
- Dead (stale lock after crash/SIGKILL) -> verify inode unchanged, unlink, and atomically reclaim
- Release lock safely on shutdown() and process exit (verifying token+pid match so another owner's lock is never unlinked)
- Add unit tests in agent-node/src/node-instance-lock.test.ts covering acquire/release, duplicate rejection, stale reclaim, symlink fail-closed, token-guarded release, and cli.ts wiring
Diff-Shape: +553/-1
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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f1d266e413
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if (check.dev === existing.dev && check.ino === existing.ino) { | ||
| unlinkSync(lockPath); |
There was a problem hiding this comment.
Serialize stale-lock reclamation across competing starters
On Linux/macOS, two processes starting concurrently after a crash can both successfully acquire this lock. Both can read and lstatSync the same stale inode; if one is descheduled before unlinkSync, the other can unlink the stale file and create its new live lock. The first then resumes, unlinks that new lock using its previously captured stat result, and successfully creates another lock. Keeping the descriptor open does not prevent unlinking on these platforms, so both nodes proceed into SSE/inbox processing—the duplicate startup this change is intended to prevent. Serialize reclamation with a separate atomic guard or use an OS-backed locking primitive; the inode check and unlink are not atomic.
Useful? React with 👍 / 👎.
| const lockRes = acquireNodeInstanceLock(NODE_DIR, { | ||
| alias: ALIAS, | ||
| nodeId: fileConfig.node_id ?? null, |
There was a problem hiding this comment.
Scope the lock to individual nodes in legacy config layouts
For nodes using the still-supported .anet/profiles/<alias>.json fallback (cli.ts:643–654), NODE_DIR is the shared .anet/profiles directory, so starting one node now prevents every other legacy-profile node in that project from starting, even when their aliases and identities differ. The same collision occurs with distinct --config files stored in one directory. The existing NODE_STATE_DIR resolution explicitly handles configs outside the canonical per-node layout; use an equivalent per-node lock location rather than assuming each config's parent directory belongs to a single node.
Useful? React with 👍 / 👎.
| if (holderPid !== null && isPidAlive(holderPid)) { | ||
| return { | ||
| ok: false, | ||
| reason: "already_running", |
There was a problem hiding this comment.
Verify process identity before treating a stale PID as its owner
After an unclean exit, if the recorded PID is reused by another process—or by the replacement node after a reboot/container restart—process.kill(pid, 0) succeeds and this branch rejects startup even though the original lock owner is gone. In the self-PID case, every startup with that recycled PID rejects itself. This defeats automatic crash recovery and requires manually removing the lock or waiting for the unrelated process to exit. Persist and verify a process-incarnation identifier, such as boot identity plus process start time, rather than treating PID existence alone as proof that the original owner is still running.
Useful? React with 👍 / 👎.
…n agent-node.lock Addressing review feedback on #3879: - Add explicit pre-open lstatSync() check before openSync so symlinks / non-regular files are refused on Windows where constants.O_NOFOLLOW === 0 - Record Linux boot_id and check /proc/<pid>/cmdline on Linux so rebooted hosts or unrelated reused PIDs are reclaimed cleanly instead of falsely refusing startup (conservative fallback to isPidAlive on macOS/Windows/unreadable /proc) - Add 2 unit tests covering reboot boot_id mismatch and Linux PID reuse Diff-Shape: +111/-2
…ink guard - Pass lockPath into formatDuplicateNodeMessage so refusal prints the real absolute lock file path (;如果确认它没在运行,删除 <lockPath> 后再启动). - Allow overriding O_NOFOLLOW flag (noFollowFlag: 0) and add a unit test proving symlinked and dangling-symlink agent-node.lock files still fail closed via lstatSync when constants.O_NOFOLLOW is unavailable (Windows). - Update formatDuplicateNodeMessage (with/without alias & PID) and duplicate start assertions, and refresh scripts/test-inventory-baseline.json. Diff-Shape: +69/-22
Summary
Implements Layer 1 of duplicate-node start protection (approved by ANet负责人, Layer 2 cross-host /
--takeoverintentionally deferred):agent-node/src/node-instance-lock.ts:<nodeDir>/agent-node.lock(O_CREAT | O_EXCL | O_RDWR | O_NOFOLLOW,0o600), writes{ version, pid, alias, node_id, hostname, started_at, token }, and holds the fd open for the lifetime of the process (zero extra child processes; works on Linux, macOS, and Windows).这个 AI 已经在这台机器上运行(PID <pid>),不用再启动;要重启请先 anet node stop <alias>kill -9), verifies inode match, cleans up the stale lock, and atomically reclaims it.release()verifies exacttoken + pidmatch before unlinking so it never removes another owner's lock.agent-node/src/cli.tsright afterNODE_DIRis resolved (before Hub registration / SSE / runtime startup), with automatic cleanup inprocess.on("exit")andshutdown().agent-node/src/node-instance-lock.test.ts.Verification
bun test agent-node/src/node-instance-lock.test.ts(7/7 pass)cc 通信欧 for review.