|
| 1 | +/** |
| 2 | + * Shutdown supervisor process. |
| 3 | + * |
| 4 | + * This process watches a lifeline (stdin/IPC). When the parent dies, the |
| 5 | + * lifeline closes and the supervisor performs best-effort cleanup: |
| 6 | + * - LOCAL: kill Chrome + remove temp profile (when keepAlive is false) |
| 7 | + * - STAGEHAND_API: request session release (when keepAlive is false) |
| 8 | + */ |
| 9 | + |
| 10 | +import Browserbase from "@browserbasehq/sdk"; |
| 11 | +import type { |
| 12 | + ShutdownSupervisorConfig, |
| 13 | + ShutdownSupervisorMessage, |
| 14 | +} from "../types/private/shutdown"; |
| 15 | +import { cleanupLocalBrowser } from "./cleanupLocal"; |
| 16 | + |
| 17 | +const SIGKILL_POLL_MS = 500; |
| 18 | +const SIGKILL_TIMEOUT_MS = 10_000; |
| 19 | +const PID_POLL_INTERVAL_MS = 500; |
| 20 | + |
| 21 | +let armed = false; |
| 22 | +let config: ShutdownSupervisorConfig | null = null; |
| 23 | +let cleanupPromise: Promise<void> | null = null; |
| 24 | + |
| 25 | +const exit = (code = 0): void => { |
| 26 | + try { |
| 27 | + process.exit(code); |
| 28 | + } catch { |
| 29 | + // ignore |
| 30 | + } |
| 31 | +}; |
| 32 | + |
| 33 | +const safeKill = async (pid: number): Promise<void> => { |
| 34 | + const isAlive = (): boolean => { |
| 35 | + try { |
| 36 | + process.kill(pid, 0); |
| 37 | + return true; |
| 38 | + } catch { |
| 39 | + return false; |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + if (!isAlive()) return; |
| 44 | + try { |
| 45 | + process.kill(pid, "SIGTERM"); |
| 46 | + } catch { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + const deadline = Date.now() + SIGKILL_TIMEOUT_MS; |
| 51 | + while (Date.now() < deadline) { |
| 52 | + await new Promise((resolve) => setTimeout(resolve, SIGKILL_POLL_MS)); |
| 53 | + if (!isAlive()) return; |
| 54 | + } |
| 55 | + try { |
| 56 | + process.kill(pid, "SIGKILL"); |
| 57 | + } catch { |
| 58 | + // best-effort |
| 59 | + } |
| 60 | +}; |
| 61 | + |
| 62 | +let pidGone = false; |
| 63 | +let pidPollTimer: NodeJS.Timeout | null = null; |
| 64 | + |
| 65 | +const startPidPolling = (pid: number): void => { |
| 66 | + if (pidPollTimer) return; |
| 67 | + pidPollTimer = setInterval(() => { |
| 68 | + try { |
| 69 | + process.kill(pid, 0); |
| 70 | + } catch { |
| 71 | + pidGone = true; |
| 72 | + if (pidPollTimer) { |
| 73 | + clearInterval(pidPollTimer); |
| 74 | + pidPollTimer = null; |
| 75 | + } |
| 76 | + } |
| 77 | + }, PID_POLL_INTERVAL_MS); |
| 78 | +}; |
| 79 | + |
| 80 | +const cleanupLocal = async ( |
| 81 | + cfg: Extract<ShutdownSupervisorConfig, { kind: "LOCAL" }>, |
| 82 | +) => { |
| 83 | + if (cfg.keepAlive) return; |
| 84 | + await cleanupLocalBrowser({ |
| 85 | + killChrome: cfg.pid && !pidGone ? () => safeKill(cfg.pid) : undefined, |
| 86 | + userDataDir: cfg.userDataDir, |
| 87 | + createdTempProfile: cfg.createdTempProfile, |
| 88 | + preserveUserDataDir: cfg.preserveUserDataDir, |
| 89 | + }); |
| 90 | +}; |
| 91 | + |
| 92 | +const cleanupBrowserbase = async ( |
| 93 | + cfg: Extract<ShutdownSupervisorConfig, { kind: "STAGEHAND_API" }>, |
| 94 | +) => { |
| 95 | + if (cfg.keepAlive) return; |
| 96 | + if (!cfg.apiKey || !cfg.projectId || !cfg.sessionId) return; |
| 97 | + try { |
| 98 | + const bb = new Browserbase({ apiKey: cfg.apiKey }); |
| 99 | + await bb.sessions.update(cfg.sessionId, { |
| 100 | + status: "REQUEST_RELEASE", |
| 101 | + projectId: cfg.projectId, |
| 102 | + }); |
| 103 | + } catch { |
| 104 | + // best-effort cleanup |
| 105 | + } |
| 106 | +}; |
| 107 | + |
| 108 | +const runCleanup = (): Promise<void> => { |
| 109 | + if (!cleanupPromise) { |
| 110 | + cleanupPromise = (async () => { |
| 111 | + const cfg = config; |
| 112 | + if (!cfg || !armed) return; |
| 113 | + armed = false; |
| 114 | + if (cfg.kind === "LOCAL") { |
| 115 | + await cleanupLocal(cfg); |
| 116 | + return; |
| 117 | + } |
| 118 | + if (cfg.kind === "STAGEHAND_API") { |
| 119 | + await cleanupBrowserbase(cfg); |
| 120 | + } |
| 121 | + })(); |
| 122 | + } |
| 123 | + return cleanupPromise; |
| 124 | +}; |
| 125 | + |
| 126 | +const onLifelineClosed = () => { |
| 127 | + void runCleanup().finally(() => exit(0)); |
| 128 | +}; |
| 129 | + |
| 130 | +const onMessage = (raw: unknown) => { |
| 131 | + if (!raw || typeof raw !== "object") return; |
| 132 | + const msg = raw as ShutdownSupervisorMessage; |
| 133 | + if (msg.type === "config") { |
| 134 | + config = msg.config ?? null; |
| 135 | + armed = Boolean(config) && config?.keepAlive === false; |
| 136 | + if (armed && config?.kind === "LOCAL" && config?.pid) { |
| 137 | + startPidPolling(config.pid); |
| 138 | + } |
| 139 | + try { |
| 140 | + process.send?.({ type: "ready" }); |
| 141 | + } catch { |
| 142 | + // ignore IPC failures |
| 143 | + } |
| 144 | + return; |
| 145 | + } |
| 146 | + if (msg.type === "exit") { |
| 147 | + armed = false; |
| 148 | + exit(0); |
| 149 | + } |
| 150 | +}; |
| 151 | + |
| 152 | +// Keep stdin open as a lifeline to the parent process. |
| 153 | +try { |
| 154 | + process.stdin.resume(); |
| 155 | + process.stdin.on("end", onLifelineClosed); |
| 156 | + process.stdin.on("close", onLifelineClosed); |
| 157 | + process.stdin.on("error", onLifelineClosed); |
| 158 | +} catch { |
| 159 | + // ignore |
| 160 | +} |
| 161 | + |
| 162 | +process.on("disconnect", onLifelineClosed); |
| 163 | +process.on("message", onMessage); |
0 commit comments