Files
openclaw/src/shared/pid-alive.ts
jeffr 6eaf2baa57 fix: detect zombie processes in isPidAlive on Linux
kill(pid, 0) succeeds for zombie processes, causing the gateway lock
to treat a zombie lock owner as alive. Read /proc/<pid>/status on
Linux to check for 'Z' (zombie) state before reporting the process
as alive. This prevents the lock from being held indefinitely by a
zombie process during gateway restart.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-22 10:38:32 +01:00

34 lines
758 B
TypeScript

import fsSync from "node:fs";
/**
* Check if a process is a zombie on Linux by reading /proc/<pid>/status.
* Returns false on non-Linux platforms or if the proc file can't be read.
*/
function isZombieProcess(pid: number): boolean {
if (process.platform !== "linux") {
return false;
}
try {
const status = fsSync.readFileSync(`/proc/${pid}/status`, "utf8");
const stateMatch = status.match(/^State:\s+(\S)/m);
return stateMatch?.[1] === "Z";
} catch {
return false;
}
}
export function isPidAlive(pid: number): boolean {
if (!Number.isFinite(pid) || pid <= 0) {
return false;
}
try {
process.kill(pid, 0);
} catch {
return false;
}
if (isZombieProcess(pid)) {
return false;
}
return true;
}