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