openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
156 lines (155 loc) • 4.85 kB
JavaScript
import { r as signalProcessTree } from "./kill-tree-DlMFq2XA.mjs";
import { t as hasErrnoCode } from "./errno-DFcoqIT9.mjs";
import { i as isProcessGroupGoneByCensus } from "./service-child-group-ownership-BSRydAAE.mjs";
import { readFile, readdir } from "node:fs/promises";
import { inspectChildWaitState, isSupported, reapChild } from "@openclaw/proc-safe/reaper";
//#region src/process/scoped-child-reaper.ts
const scheduledChildren = /* @__PURE__ */ new WeakSet();
const groups = /* @__PURE__ */ new Map();
const EXIT_DELAY_MS = 25;
const POLL_INTERVAL_MS = 1e3;
const CLEANUP_DEADLINE_MS = 3e4;
let timer;
let nextScanAt = 0;
let scanning = false;
function schedule(e) {
const t = performance.now() + e;
if (timer && nextScanAt <= t) return;
clearTimeout(timer);
nextScanAt = t;
timer = setTimeout(() => void tick(), e).unref();
}
function onExit() {
schedule(EXIT_DELAY_MS);
}
async function tick() {
timer = void 0;
if (scanning) {
schedule(EXIT_DELAY_MS);
return;
}
scanning = true;
try {
for (const [e, t] of groups) {
if (performance.now() >= t) {
groups.delete(e);
continue;
}
try {
process.kill(-e, 0);
} catch (t) {
if (hasErrnoCode(t, "ESRCH")) groups.delete(e);
}
}
if (groups.size === 0) return;
for (const e of await readdir("/proc/self/task")) {
let t;
try {
t = await readFile(`/proc/self/task/${e}/children`, "utf8");
} catch {
continue;
}
for (const e of t.trim().split(/\s+/u).filter(Boolean)) {
const t = Number(e);
let n;
try {
n = await readFile(`/proc/${t}/stat`, "utf8");
} catch {
continue;
}
const [r, i, a] = n.slice(n.lastIndexOf(")") + 2).split(" ");
if (!groups.has(Number(a)) || t === Number(a) || Number(i) !== process.pid || r !== "Z") continue;
try {
if (!isSupported()) {
groups.clear();
return;
}
if (inspectChildWaitState(t).kind === "exited") reapChild(t);
} catch {}
}
}
} catch {} finally {
scanning = false;
if (groups.size > 0) schedule(POLL_INTERVAL_MS);
else {
clearTimeout(timer);
timer = void 0;
process.off("SIGCHLD", onExit);
}
}
}
/** Retain only the terminated group's adopted children after libuv consumes root exit. */
function scheduleAdoptedChildZombieReapAfterExit(e, t, n = CLEANUP_DEADLINE_MS) {
if (process.platform !== "linux" || !t || e.pid === void 0 || scheduledChildren.has(e)) return;
const r = e.pid;
scheduledChildren.add(e);
const start = () => {
if (groups.size === 0) process.on("SIGCHLD", onExit);
groups.set(r, performance.now() + Math.max(CLEANUP_DEADLINE_MS, n));
onExit();
};
if (e.exitCode !== null || e.signalCode !== null) start();
else e.once("exit", start);
}
//#endregion
//#region src/process/child-process-tree.ts
function shouldDetachChildForProcessTree() {
return process.platform !== "win32";
}
/**
* Linux hosts can deny negative-PID kill(2) (OpenShell's seccomp rule) with EPERM. A kernel EPERM
* (a member owned by another UID) proves the group has members, so the denial is only believed
* when a group that cannot exist is refused with EPERM too, for the same signal as the failing
* call (a filter can allow signal 0 and deny real signals). The kernel answers that group with
* ESRCH, so nothing is delivered. Not cached: the answer is per host call.
*/
function isDeniedProcessGroupProbe(e, t = 0) {
if (process.platform !== "linux" || !hasErrnoCode(e, "EPERM")) return false;
try {
process.kill(-4194305, t);
return false;
} catch (e) {
return hasErrnoCode(e, "EPERM");
}
}
/** Decides group extinction for `isChildProcessTreeAlive` and spawn-broker cleanup; other probes keep their own EPERM policies. */
function isProcessGroupGone(e) {
try {
process.kill(-e, 0);
return false;
} catch (t) {
if (hasErrnoCode(t, "ESRCH")) return true;
return isDeniedProcessGroupProbe(t) ? isProcessGroupGoneByCensus(e) : false;
}
}
function isChildProcessTreeAlive(e) {
if (typeof e.pid !== "number" || e.pid <= 0) return false;
if (shouldDetachChildForProcessTree()) return !isProcessGroupGone(e.pid);
try {
process.kill(e.pid, 0);
return true;
} catch (e) {
return !hasErrnoCode(e, "ESRCH");
}
}
function signalChildProcessTree(e, t, n) {
if (typeof e.pid === "number" && e.pid > 0) {
const r = shouldDetachChildForProcessTree();
signalProcessTree(e.pid, t, {
detached: r,
onComplete: n
});
scheduleAdoptedChildZombieReapAfterExit(e, r);
return;
}
try {
e.kill(t);
} finally {
n?.();
}
}
function forceKillChildProcessTree(e) {
signalChildProcessTree(e, "SIGKILL");
}
//#endregion
export { shouldDetachChildForProcessTree as a, isProcessGroupGone as i, isChildProcessTreeAlive as n, signalChildProcessTree as o, isDeniedProcessGroupProbe as r, scheduleAdoptedChildZombieReapAfterExit as s, forceKillChildProcessTree as t };