(label: string, watchdog: WatchdogOptions = {})
| 52 | * SIGKILLed (#1231). |
| 53 | */ |
| 54 | export function installCommandSupervision(label: string, watchdog: WatchdogOptions = {}): CommandSupervision { |
| 55 | // Liveness watchdog: a separate process that SIGKILLs us if our event loop |
| 56 | // stops turning for too long (a wedged synchronous loop). Self-disables on |
| 57 | // CODEGRAPH_NO_WATCHDOG. |
| 58 | const liveness = installMainThreadWatchdog(watchdog); |
| 59 | |
| 60 | // PPID watchdog: detect that the parent (or the host threaded past the |
| 61 | // relaunch shim) died and we've been orphaned, then exit instead of leaking. |
| 62 | // Baseline from the CLI entry's earliest-possible capture — reading |
| 63 | // process.ppid here would miss a launcher killed during startup (#1185). |
| 64 | const originalPpid = EARLY_PPID; |
| 65 | const hostPpid = parseHostPpid(process.env[HOST_PPID_ENV]); |
| 66 | const pollMs = parsePpidPollMs(process.env.CODEGRAPH_PPID_POLL_MS); |
| 67 | let ppidTimer: ReturnType<typeof setInterval> | null = null; |
| 68 | if (pollMs > 0) { |
| 69 | ppidTimer = setInterval(() => { |
| 70 | const reason = supervisionLostReason({ |
| 71 | originalPpid, |
| 72 | currentPpid: process.ppid, |
| 73 | hostPpid, |
| 74 | isAlive: isProcessAlive, |
| 75 | }); |
| 76 | if (reason) { |
| 77 | try { |
| 78 | process.stderr.write(`[CodeGraph ${label}] Parent process exited (${reason}); aborting.\n`); |
| 79 | } catch { /* stderr gone with the parent — exit anyway */ } |
| 80 | process.exit(1); |
| 81 | } |
| 82 | }, pollMs); |
| 83 | // Never let the watchdog itself keep the process alive past its real work. |
| 84 | ppidTimer.unref(); |
| 85 | } |
| 86 | |
| 87 | let stopped = false; |
| 88 | return { |
| 89 | stop(): void { |
| 90 | if (stopped) return; |
| 91 | stopped = true; |
| 92 | if (ppidTimer) clearInterval(ppidTimer); |
| 93 | liveness?.stop(); |
| 94 | }, |
| 95 | }; |
| 96 | } |
no test coverage detected