UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

136 lines • 6.39 kB
/** * Process-table access for the host child reaper. * * Three operations, each with exactly one job: * - `listDirectChildren` enumerates the DIRECT children of this process * (darwin: libproc `proc_listchildpids`, which lists zombies too; linux: * a `/proc/<pid>/stat` ppid scan). Never a `ps` spawn - a reaper that * spawns children to find children is the bug it is meant to fix (#1507). * - `isWaitable` is the ORACLE: `waitid(P_PID, pid, WEXITED|WNOHANG|WNOWAIT)` * peeks at the exit status WITHOUT consuming it, so an observation never * steals a child from the thread that owns it. `proc_pidinfo` is NOT used * for this: it reports nothing trustworthy about a zombie. * - `reapExited` consumes exactly one pid with `waitpid(pid, WNOHANG)`. * `waitpid(-1, ...)` is never called: it would steal an arbitrary child * from whichever thread was waiting for it. * * RUNTIME BOUNDARY (the repository's one sanctioned reason for a dynamic * import, same shape as `ownership-safe-lock.ts`'s `bun:sqlite` loader): the * bindings need `bun:ffi`, a Bun-only builtin. A TOP-LEVEL import of it makes * this module - and therefore every host that reaches it - unloadable on Node * (`ERR_UNSUPPORTED_ESM_URL_SCHEME: Received protocol 'bun:'`, verified). The * specifier is therefore imported behind the runtime gate in * `loadChildReaperSyscalls`, which resolves to `undefined` on Node so the * reaper turns itself off with one warning. Everything else here is top-level. */ import { readdirSync, readFileSync } from "node:fs"; /** waitid/waitpid option bits and idtype, which differ between the two platforms. */ const P_PID = 1; const WNOHANG = 0x01; const WEXITED = 0x04; const WNOWAIT = { darwin: 0x20, linux: 0x0100_0000 }; /** Byte offset of `si_pid` inside `siginfo_t` (darwin packs it right after si_code). */ const SI_PID_OFFSET = { darwin: 12, linux: 16 }; /** A host with more live children than this is pathological; the scan stays bounded. */ const MAX_TRACKED_CHILDREN = 4096; function supportedPlatform(platform) { return platform === "darwin" || platform === "linux" ? platform : undefined; } /** * Loads the platform bindings, or `undefined` when this runtime cannot reap * (Node, Windows, or a libc without the symbols). The platform and the runtime * are both decided BEFORE the `bun:ffi` specifier is reached, so a Node or * win32 host never asks its loader for a module it cannot resolve. */ export async function loadChildReaperSyscalls(platform = process.platform) { const supported = supportedPlatform(platform); if (supported === undefined) return undefined; if (typeof globalThis.Bun === "undefined") return undefined; const { dlopen, FFIType, ptr } = await import("bun:ffi"); const library = supported === "darwin" ? "libSystem.B.dylib" : "libc.so.6"; const wait = dlopen(library, { waitid: { args: [FFIType.i32, FFIType.u32, FFIType.ptr, FFIType.i32], returns: FFIType.i32 }, waitpid: { args: [FFIType.i32, FFIType.ptr, FFIType.i32], returns: FFIType.i32 }, }); const info = new Uint8Array(512); const infoView = new DataView(info.buffer); const status = new Int32Array(1); const peekOptions = WEXITED | WNOHANG | WNOWAIT[supported]; const siPidOffset = SI_PID_OFFSET[supported]; return { listDirectChildren: supported === "darwin" ? darwinChildren(dlopen, FFIType, ptr) : linuxChildren, isWaitable(pid) { // Zeroed before every call: with WNOHANG the kernel leaves si_pid at 0 // when the child has not exited, and that is the whole signal. info.fill(0); const result = wait.symbols.waitid(P_PID, pid, ptr(info), peekOptions); return result === 0 && infoView.getInt32(siPidOffset, true) === pid; }, reapExited(pid) { if (pid <= 0) throw new Error(`child reaper refuses to wait on pid ${pid}`); return wait.symbols.waitpid(pid, ptr(status), WNOHANG) === pid; }, describe: supported === "darwin" ? darwinName(dlopen, FFIType, ptr) : linuxName, }; } function darwinChildren(dlopen, FFIType, ptr) { const libproc = dlopen("libSystem.B.dylib", { proc_listchildpids: { args: [FFIType.i32, FFIType.ptr, FFIType.i32], returns: FFIType.i32 }, }); const pids = new Int32Array(MAX_TRACKED_CHILDREN); return () => { const found = libproc.symbols.proc_listchildpids(process.pid, ptr(pids), pids.byteLength); return found <= 0 ? [] : Array.from(pids.subarray(0, Math.min(found, pids.length))); }; } function darwinName(dlopen, FFIType, ptr) { const libproc = dlopen("libSystem.B.dylib", { proc_name: { args: [FFIType.i32, FFIType.ptr, FFIType.u32], returns: FFIType.i32 }, }); const name = new Uint8Array(256); const decoder = new TextDecoder(); return (pid) => { name.fill(0); const written = libproc.symbols.proc_name(pid, ptr(name), name.length); return written <= 0 ? "" : decoder.decode(name.subarray(0, written)); }; } /** `/proc/<pid>/stat` field 4 is the parent pid; zombies keep their entry. */ function linuxChildren() { const children = []; for (const entry of readdirSync("/proc")) { const pid = Number(entry); if (!Number.isInteger(pid) || pid <= 0) continue; if (parentOf(pid) === process.pid) children.push(pid); if (children.length >= MAX_TRACKED_CHILDREN) break; } return children; } function linuxStat(pid) { try { return readFileSync(`/proc/${pid}/stat`, "utf8"); } catch { // The process exited between the directory listing and this read. return undefined; } } function parentOf(pid) { // comm is parenthesised and may contain spaces, so parse after its last ')'. const stat = linuxStat(pid); const tail = stat?.slice(stat.lastIndexOf(")") + 2).split(" "); return tail === undefined ? undefined : Number(tail[1]); } function linuxName(pid) { const stat = linuxStat(pid); const open = stat?.indexOf("(") ?? -1; const close = stat?.lastIndexOf(")") ?? -1; return stat === undefined || open === -1 || close <= open ? "" : stat.slice(open + 1, close); } //# sourceMappingURL=child-reaper-syscalls.js.map