proc: a Linux zombie no longer holds a lock (#1198)

yetihan (#1198): a lock whose owner is a Linux zombie was held for
good. process.kill(pid, 0) succeeds on a zombie, so 15 packages' lock
and liveness checks counted an exited, unreaped owner as alive.

- scripts/proc.mjs: processStatus(pid) is dead on ESRCH, and on Linux
  also when /proc/<pid>/stat says Z or X; EPERM is "permission", any
  other error "unknown". processAlive / processExists keep each
  caller's old EPERM and unknown-error meaning.
- scripts/sync-proc.mjs copies it into each package (--check in the
  tests); every package's own check keeps its old semantics, now with
  zombies dead. agent-console's daemon bundle and window-warmup's dist
  are rebuilt.
- 15 packages bumped.

Review run at 28ef6622 (mote, HOME=$(mktemp -d)):
- node scripts/sync-proc.mjs --check: ok.
- bun test (whole repo): 8934 pass, 0 fail.
- bun scripts/check.mjs: every package ok.
- window-warmup's build reproduces its dist/plugin.js byte for byte.
  agent-console's bundle diff read line by line: the proc module plus
  esbuild's renumbered imports, nothing else (esbuild isn't installed
  on mote, so it wasn't rebuilt there).
- Real zombie on Linux (WSL, magpie-desktop): a bash child left
  unreaped by an exec'd sleep, /proc stat "Z": the old kill(0) check
  says alive, processStatus says dead; the running process says alive.

Not tried: each package's lock contention end to end.
This commit is contained in:
zhou.han
2026-11-16 12:19:09 +08:00
committed by GitHub
parent 0b0b180957
commit 9e86a2c7b7
63 changed files with 1391 additions and 158 deletions
+2 -3
View File
@@ -17,6 +17,7 @@ import { IMPORT_FROM, importedAccounts, tokenFileText } from "./import.mjs"
import { AGY_MANUAL, installAgy, keepAgyCurrent } from "./install.mjs"
import { ensureGeminiArrayItems } from "./array-items.mjs"
import { expandToolSchema } from "./tool-schema.mjs"
import { processExists } from "./proc.mjs"
import { fitTools } from "./tool-budget.mjs"
import { cloakUser, hint, outgoing, turnsAway } from "./cloak.mjs"
import { modelUsageCursor, modelUsageAfter, sessionDbPath } from "./step-usage.mjs"
@@ -2930,9 +2931,7 @@ function withStateLock(file, fn) {
if (Date.now() - fs.statSync(lock).mtimeMs > 5_000) {
const owner = JSON.parse(fs.readFileSync(lock, "utf8"))
if (Number.isSafeInteger(owner.pid) && owner.pid > 0) {
try { process.kill(owner.pid, 0) } catch (error) {
if (error.code === "ESRCH") fs.unlinkSync(lock)
}
if (!processExists(owner.pid)) fs.unlinkSync(lock)
}
}
} catch {}
+1
View File
@@ -8,6 +8,7 @@
".": "./opencode2.mjs"
},
"files": [
"proc.mjs",
"index.mjs",
"opencode.mjs",
"opencode2.mjs",
+59
View File
@@ -0,0 +1,59 @@
// GENERATED by scripts/sync-proc.mjs from scripts/proc.mjs. Do not edit package copies.
import { readFileSync } from "node:fs";
import { join } from "node:path";
const errCode = (error) => (error && typeof error === "object" ? error.code : undefined);
function validPid(pid) {
return Number.isSafeInteger(pid) && pid > 0;
}
export function processZombie(pid, {
procRoot = "/proc",
readFile = (file) => readFileSync(file, "utf8"),
} = {}) {
if (!validPid(pid)) return false;
let stat;
try {
stat = readFile(join(procRoot, String(pid), "stat"));
} catch {
return false;
}
const commEnd = typeof stat === "string" ? stat.lastIndexOf(")") : -1;
if (commEnd < 0) return false;
const state = stat.slice(commEnd + 1).trim()[0];
return state === "Z" || state === "X";
}
export function processAlive(pid, deps = {}) {
const state = processStatus(pid, deps).state;
return state === "alive" || state === "permission";
}
export function processExists(pid, deps = {}) {
const state = processStatus(pid, deps).state;
return state !== "dead" && state !== "invalid";
}
export function processStatus(pid, deps = {}) {
const kill = deps.kill ?? ((target, signal) => process.kill(target, signal));
if (!validPid(pid)) return { state: "invalid" };
let permission = false;
try {
kill(pid, 0);
} catch (error) {
const code = errCode(error);
if (code === "ESRCH") return { state: "dead" };
if (code !== "EPERM") return { state: "unknown", error };
permission = true;
}
if ((deps.platform ?? process.platform) !== "linux") {
return permission ? { state: "permission" } : { state: "alive" };
}
if (processZombie(pid, deps)) return { state: "dead" };
return permission ? { state: "permission" } : { state: "alive" };
}
export function processProbe(pid, deps = {}) {
return processStatus(pid, deps).state;
}
+3
View File
@@ -40,6 +40,7 @@ export const STEP_USAGE_FILE = path.join(REPO_DIR, "step-usage.mjs")
export const SIGNIN_FILE = path.join(REPO_DIR, "signin.mjs")
export const TOOL_SCHEMA_FILE = path.join(REPO_DIR, "tool-schema.mjs")
export const TOOL_BUDGET_FILE = path.join(REPO_DIR, "tool-budget.mjs")
export const PROC_FILE = path.join(REPO_DIR, "proc.mjs")
export const INSTALL_FILE = path.join(REPO_DIR, "install.mjs")
export const ACCOUNT_FILE = path.join(REPO_DIR, "account.mjs")
export const IMPORT_FILE = path.join(REPO_DIR, "import.mjs")
@@ -563,6 +564,7 @@ export async function createBridgeKit(options = {}) {
const signinUrl = pathToFileURL(SIGNIN_FILE).href
const toolSchemaUrl = pathToFileURL(TOOL_SCHEMA_FILE).href
const toolBudgetUrl = pathToFileURL(TOOL_BUDGET_FILE).href
const procUrl = pathToFileURL(PROC_FILE).href
const installUrl = pathToFileURL(INSTALL_FILE).href
const accountUrl = pathToFileURL(ACCOUNT_FILE).href
const importUrl = pathToFileURL(IMPORT_FILE).href
@@ -583,6 +585,7 @@ export async function createBridgeKit(options = {}) {
else if (key === "./signin.mjs") module = sourceModule(SIGNIN_FILE, "", signinUrl)
else if (key === "./tool-schema.mjs") module = sourceModule(TOOL_SCHEMA_FILE, "", toolSchemaUrl)
else if (key === "./tool-budget.mjs") module = sourceModule(TOOL_BUDGET_FILE, "", toolBudgetUrl)
else if (key === "./proc.mjs") module = sourceModule(PROC_FILE, "", procUrl)
else if (key === "./install.mjs") module = sourceModule(INSTALL_FILE, "", installUrl)
else if (key === "./account.mjs") module = sourceModule(ACCOUNT_FILE, "", accountUrl)
else if (key === "./import.mjs") module = sourceModule(IMPORT_FILE, "", importUrl)