Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions scripts/lib/process.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,18 @@ export function getProcessIdentity(pid) {
}
}

export function validateProcessIdentity(pid, expectedIdentity) {
export function validateProcessIdentity(pid, expectedIdentity, options = {}) {
const identityImpl = options.identityImpl ?? getProcessIdentity;
try {
return getProcessIdentity(pid) === expectedIdentity;
} catch {
return identityImpl(pid) === expectedIdentity;
} catch (error) {
// A denied probe (sandboxed `ps`, unreadable /proc entry) means the identity
// cannot be established, not that the process is gone. Fall back to the
// zero-signal liveness probe, which still reports ESRCH for a dead PID.
// PID-reuse detection is only given up when identity is unknowable at all.
if (error?.code === "EPERM" || error?.code === "EACCES") {
return isProcessAlive(pid, options);
Comment on lines +167 to +168

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Do not treat an unverified PID as an identity match

When the identity probe is denied and the original PID has been reused, the zero-signal check succeeds for the unrelated process, so this function incorrectly reports that its identity matches. cancelClaudeProcess() relies on this result at lines 940 and 961 before sending SIGTERM/SIGKILL to the process group, meaning cancellation can terminate an unrelated process; preserve an unknown/denied state so liveness fallback is used by the stale-job reaper without bypassing cancellation's identity check, and cover that caller behavior.

AGENTS.md reference: AGENTS.md:L5-L5

Useful? React with 👍 / 👎.

}
return false;
}
}
Expand Down
38 changes: 38 additions & 0 deletions tests/process.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -387,4 +387,42 @@ describe("validateProcessIdentity", () => {
it("returns false for non-existent PID", () => {
assert.equal(validateProcessIdentity(99999999, "any"), false);
});

it("falls back to the liveness probe when the identity probe is denied", () => {
const deniedIdentity = () => {
throw Object.assign(new Error("spawnSync ps EPERM"), { code: "EPERM" });
};

assert.equal(
validateProcessIdentity(12345, "recorded-identity", {
identityImpl: deniedIdentity,
killImpl: () => {
throw Object.assign(new Error("operation not permitted"), { code: "EPERM" });
},
}),
true,
);

assert.equal(
validateProcessIdentity(12345, "recorded-identity", {
identityImpl: deniedIdentity,
killImpl: () => {
throw Object.assign(new Error("no such process"), { code: "ESRCH" });
},
}),
false,
);
});

it("keeps a failed identity probe that is not a denial classified as dead", () => {
assert.equal(
validateProcessIdentity(12345, "recorded-identity", {
identityImpl: () => {
throw new Error("ps: exit=1");
},
killImpl: () => true,
}),
false,
);
});
});