* Run as the detached shared daemon (process spawned with * `CODEGRAPH_DAEMON_INTERNAL=1`). Arbitrate the O_EXCL lock, then either * become the daemon (bind the socket, serve forever) or — if a live daemon * already holds the lock — exit so we don't leak a redundant process. * * No PP
()
| 331 | * and reaps itself via client-refcount + idle timeout (see {@link Daemon}). |
| 332 | */ |
| 333 | private async startDaemonProcess(): Promise<void> { |
| 334 | const root = resolveDaemonRoot(this.projectPath) ?? this.projectPath ?? process.cwd(); |
| 335 | for (let attempt = 0; attempt < TAKEOVER_MAX_RETRIES; attempt++) { |
| 336 | const lock = tryAcquireDaemonLock(root); |
| 337 | |
| 338 | if (lock.kind === 'acquired') { |
| 339 | const daemon = new Daemon(root); |
| 340 | await daemon.start(); |
| 341 | this.daemon = daemon; |
| 342 | this.mode = 'daemon'; |
| 343 | // The detached daemon has no PPID watchdog or stdin lifeline, so a |
| 344 | // wedged main thread would pin a core forever (#850). The liveness |
| 345 | // watchdog is its only recovery path. |
| 346 | this.livenessWatchdog = installMainThreadWatchdog(); |
| 347 | return; // the net.Server keeps the process alive |
| 348 | } |
| 349 | |
| 350 | // Taken. If the holder is alive, another daemon already serves (or is |
| 351 | // binding) — we're redundant; exit cleanly so the launcher proxies to it. |
| 352 | const existing = lock.existing; |
| 353 | if (existing && existing.pid > 0 && isProcessAlive(existing.pid)) { |
| 354 | process.stderr.write( |
| 355 | `[CodeGraph daemon] Another daemon (pid ${existing.pid}) already holds the lock; exiting.\n` |
| 356 | ); |
| 357 | process.exit(0); |
| 358 | } |
| 359 | |
| 360 | // Holder is dead (or the record is unreadable) — clear it (pid-verified, |
| 361 | // so we never delete a live daemon's lock) and retry the acquire. |
| 362 | clearStaleDaemonLock(lock.pidPath, existing?.pid); |
| 363 | await sleep(TAKEOVER_RETRY_DELAY_MS); |
| 364 | } |
| 365 | |
| 366 | process.stderr.write('[CodeGraph daemon] Could not acquire the daemon lock; exiting.\n'); |
| 367 | process.exit(0); |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Proxy mode (the common case). Serve the MCP handshake LOCALLY for instant |
no test coverage detected