The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // @effect-diagnostics nodeBuiltinImport:off - Runs synchronously in the process "exit" event, where nothing async completes. |
| 2 | import * as NodeFS from "node:fs"; |
| 3 | |
| 4 | // Matches the backend's quit budget: SIGTERM, 5 s grace, SIGKILL, 1 s wait. |
| 5 | const TERMINATE_GRACE_MS = 5_000; |
| 6 | const KILL_WAIT_MS = 1_000; |
| 7 | |
| 8 | const readProc = (path: string): string | undefined => { |
| 9 | try { |
| 10 | return NodeFS.readFileSync(path, "utf8"); |
| 11 | } catch { |
| 12 | return undefined; |
| 13 | } |
| 14 | }; |
| 15 | |
| 16 | // Chromium launches helpers from its own threads, so each thread's children file is read. |
| 17 | const childPids = (pid: number): ReadonlyArray<number> => { |
| 18 | let tasks: ReadonlyArray<string>; |
| 19 | try { |
| 20 | tasks = NodeFS.readdirSync(`/proc/${pid}/task`); |
| 21 | } catch { |
| 22 | return []; |
| 23 | } |
| 24 | return tasks.flatMap((task) => |
| 25 | (readProc(`/proc/${pid}/task/${task}/children`) ?? "") |
| 26 | .split(" ") |
| 27 | .filter((value) => value.trim() !== "") |
| 28 | .map(Number), |
| 29 | ); |
| 30 | }; |
| 31 | |
| 32 | // A zygote rewrites its argv into one space-separated title. |
| 33 | const isZygote = (pid: number): boolean => |
| 34 | (readProc(`/proc/${pid}/cmdline`) ?? "") |
| 35 | .replaceAll("\0", " ") |
| 36 | .split(" ") |
| 37 | .includes("--type=zygote"); |
| 38 | |
| 39 | // A zombie has already released its memory mappings. |
| 40 | const isRunning = (pid: number): boolean => { |
| 41 | const stat = readProc(`/proc/${pid}/stat`); |
| 42 | if (stat === undefined) return false; |
| 43 | const state = stat.charAt(stat.lastIndexOf(")") + 2); |
| 44 | return state !== "Z" && state !== "X"; |
| 45 | }; |
| 46 | |
| 47 | const signal = (pids: ReadonlyArray<number>, name: NodeJS.Signals) => { |
| 48 | for (const pid of pids) { |
| 49 | try { |
| 50 | process.kill(pid, name); |
| 51 | } catch {} |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | const waitUntilStopped = (pids: ReadonlyArray<number>, maxMs: number): ReadonlyArray<number> => { |
| 56 | const tick = new Int32Array(new SharedArrayBuffer(4)); |
| 57 | let running = pids.filter(isRunning); |
| 58 | for (let waited = 0; waited < maxMs && running.length > 0; waited += 10) { |
| 59 | Atomics.wait(tick, 0, 0, 10); |
| 60 | running = running.filter(isRunning); |
| 61 | } |
| 62 | return running; |
| 63 | }; |
| 64 | |
| 65 | /** |
| 66 | * Stops the main process's children while it is still alive. On an AppImage the image unmounts |
| 67 | * as soon as main exits, so children left running then die with SIGBUS. Linux only (/proc). |
| 68 | * Returns the pids that needed SIGKILL. |
| 69 | */ |
| 70 | export const stopChildProcessesBeforeExit = (pid: number): ReadonlyArray<number> => { |
| 71 | const children = childPids(pid); |
| 72 | signal(children, "SIGTERM"); |
| 73 | const survivors = waitUntilStopped(children, TERMINATE_GRACE_MS); |
| 74 | signal(survivors, "SIGKILL"); |
| 75 | waitUntilStopped(survivors, KILL_WAIT_MS); |
| 76 | return survivors; |
| 77 | }; |
| 78 | |
| 79 | /** |
| 80 | * Chromium's zygotes exit only after main does, so on an AppImage they outlive the mount even on |
| 81 | * an orderly quit. Kills them while main still holds the image mounted. Linux only (/proc). |
| 82 | */ |
| 83 | export const stopZygotesBeforeExit = (pid: number): void => { |
| 84 | const zygotes = childPids(pid).filter(isZygote); |
| 85 | signal(zygotes, "SIGKILL"); |
| 86 | waitUntilStopped(zygotes, KILL_WAIT_MS); |
| 87 | }; |