Skip to content

fix(scripts): treat an EPERM claim as a live lock contender - #855

Open
Abolfazlrwm wants to merge 1 commit into
simplexai-labs:mainfrom
Abolfazlrwm:fix/scripts-lock-foreign-claim
Open

Abolfazlrwm wants to merge 1 commit into
simplexai-labs:mainfrom
Abolfazlrwm:fix/scripts-lock-foreign-claim

Conversation

@Abolfazlrwm

Copy link
Copy Markdown

What and why

readClaims in scripts/managed-tools-lock.mjs liveness-checks each claim with process.kill(pid, 0). POSIX throws ESRCH for a dead PID but EPERM for a live PID owned by another user — and the code rethrew anything that was not ESRCH. Consequences:

  • On a shared tools root (root installs, a normal user's launcher reads the same root), every ensure/update/rollback/status call failed immediately with a raw EPERM: operation not permitted, kill while the root's install was live — the 30 s bakery deadline never even applied.
  • A stale claim whose PID got reused by a foreign-UID process wedged the lock the same way, forever.

EPERM still 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 in packages/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

  • New scripts/managed-tools-lock.test.ts (picked up by the existing scripts/*.test.ts wildcard in the Makefile):
    • a ESRCH claim is reclaimed and the lock is acquired;
    • a foreign-UID claim (EPERM from 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.
  • Local run: 2/2 pass, prettier + eslint clean.
  • The wider managed-tools-install.test.ts suite 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).

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.

@linger-review linger-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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). .locks and every claim are created 0o700 (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, every ensure/update/rollback threw 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-165 treats 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 from kill(pid, 0). The only case where waiting is right is a root-run install, for example via sudo with the user's HOME or LINGER_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.

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