Repository navigation
fix(scripts): treat an EPERM claim as a live lock contender - #855
Abolfazlrwm wants to merge 1 commit into
Conversation
process.kill(pid, 0) throws EPERM when the claim PID exists but belongs to another user, and readClaims rethrew it: on a shared tools root - or once a stale claim PID was reused by a foreign process - every ensure, update, rollback and status failed immediately with a cryptic EPERM, before the bakery deadline ever applied. EPERM still means the process is alive, so keep the claim and wait for its ticket like any live contender, matching native-process.ts handling of foreign-UID PIDs. A dead claim (ESRCH) is still reclaimed as before.
There was a problem hiding this comment.
Request changes (1 P1, 1 P2) · merge base 17f6016 · 1 commit, 2 files
Findings
P1: scripts/managed-tools-lock.mjs:73: every EPERM is treated as a live holder, so a stale claim whose PID was reused by another user still wedges the lock
The PR body lists two failures: a shared root, and "a stale claim whose PID got reused by a foreign-UID process wedged the lock … forever". The change fixes neither in the common case:
- PID reuse (the realistic case). The lock root is per-user by default (
~/.local/share/simplexai/tools,managed-tools-install.mjs:272)..locksand every claim are created0o700(managed-tools-install.mjs:401,managed-tools-lock.mjs:18,21). Say a crashed install left a claim with ticket N, and its PID now belongs to another user's process. Before this PR, everyensure/update/rollbackthrew EPERM right away. Now each one waits the full 30 s and then throws "Timed out waiting for managed tools installation lock". The lock stays wedged for as long as that unrelated process lives. The failure is just slower now. This matters more on Windows, which ships this module (local-computer-windows-installer.mjs:35) and reuses PIDs aggressively. - The cited precedent says the opposite.
packages/local-installation/src/native-process.ts:156-165treats an EPERM PID owned by another UID as not ours, and explains why: "ignoring a foreign-UID reused PID avoids locking out resume or stop". This PR treats it as alive, so the lockout that comment avoids comes back here. - The shared-root premise mostly can't happen. A second non-root user cannot create a claim inside another user's
0o700.locks(EACCES before the PID check runs). Root never gets EPERM fromkill(pid, 0). The only case where waiting is right is a root-run install, for example viasudowith the user'sHOMEorLINGER_TOOLS_ROOT, holding a claim in a user-owned root.
Fix: base the decision on who owns the claim, not just on the errno. Each claim directory is created by its contender, so stat(claim).uid is that contender's UID. On EPERM:
- If the claim is owned by
process.getuid(), our own process created it, so the PID has been reused. Reclaim it like ESRCH. - If it is owned by another UID (root's claim), wait as this PR does.
On Windows (getuid undefined), decide explicitly instead of defaulting to "live".
P2: scripts/managed-tools-lock.test.ts:47: no test for reclaiming a reused PID
The new tests cover "ESRCH is reclaimed" and "any EPERM is waited out". Nothing pins the PID-reuse path from the PR description. Once the P1 fix is in, add a test where process.kill throws EPERM for a claim the current user owns, and assert that the lock is acquired and the stale claim is removed. Keep the existing test for a claim owned by another UID. For example, mock stat or lstat to return a different uid.
Checked: lock dirs created 0o700 (managed-tools-install.mjs:401) · per-user default root (managed-tools-install.mjs:272) · precedent ignores foreign-UID PIDs (native-process.ts:156)
Unverified: new test run (no local deps installed, no CI run)
Process: commit author Z User <z@container> is not linked to a GitHub account · branch is 3 commits behind main, needs rebase
CI: no checks reported on this branch
Automated review of c3a93df by Claude Code.
What and why
readClaimsinscripts/managed-tools-lock.mjsliveness-checks each claim withprocess.kill(pid, 0). POSIX throwsESRCHfor a dead PID butEPERMfor a live PID owned by another user — and the code rethrew anything that was notESRCH. Consequences:ensure/update/rollback/statuscall failed immediately with a rawEPERM: operation not permitted, killwhile the root's install was live — the 30 s bakery deadline never even applied.EPERMstill means the process exists, so the claim is a live contender: keep it and wait for its ticket like any other holder. This mirrors the repo's own documented handling of the identical situation inpackages/local-installation/src/native-process.ts("A PID owned by another user cannot be a native service this user started…"). A dead claim (ESRCH) is still reclaimed as before.How it was verified
scripts/managed-tools-lock.test.ts(picked up by the existingscripts/*.test.tswildcard in the Makefile):ESRCHclaim is reclaimed and the lock is acquired;EPERMfrom the PID check) is waited out — the attempt times out with the bakery timeout error and releases only our own claim, never the live one.managed-tools-install.test.tssuite requires Node ≥ 26.5.0; my authoring environment has Node 24.x, so it fails at its version guard before reaching the lock code. CI runs the required Node and will exercise it.Boundary
No docs: internal lock semantics restored to what the bakery protocol already assumes ("Dead claims can be removed…" — and only dead ones).