* 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
()
| 670 | * and reaps itself via client-refcount + idle timeout (see {@link Daemon}). |
| 671 | */ |
| 672 | private async startDaemonProcess(): Promise<void> { |
| 673 | // In daemon mode stderr IS `.codegraph/daemon.log`; stamp every line so |
| 674 | // kills/restarts can be placed in time (#1431 — the log was undatable). |
| 675 | timestampStderrLines(); |
| 676 | // A launcher never starts a daemon for a sensitive folder (see |
| 677 | // resolveDaemonRoot). One started by hand for such a folder leaves rather |
| 678 | // than take the folder's locks and serve it. |
| 679 | const refusal = resolveServerRoot(this.projectPath ?? process.cwd()).refusal; |
| 680 | if (refusal) { |
| 681 | process.stderr.write(`[CodeGraph daemon] ${refusal}; exiting.\n`); |
| 682 | process.exit(0); |
| 683 | } |
| 684 | const root = resolveDaemonRoot(this.projectPath) ?? this.projectPath ?? process.cwd(); |
| 685 | // Read once and dropped, so nothing this daemon spawns inherits it. |
| 686 | const handoverPid = Number(process.env[DAEMON_HANDOVER_ENV]); |
| 687 | const handoverFrom = Number.isInteger(handoverPid) && handoverPid > 0 ? handoverPid : null; |
| 688 | delete process.env[DAEMON_HANDOVER_ENV]; |
| 689 | for (let attempt = 0; attempt < TAKEOVER_MAX_RETRIES; attempt++) { |
| 690 | const lock = tryAcquireDaemonLock(root); |
| 691 | |
| 692 | if (lock.kind === 'acquired') { |
| 693 | const daemon = new Daemon(root, { handoverFrom }); |
| 694 | await daemon.start(); |
| 695 | this.daemon = daemon; |
| 696 | this.mode = 'daemon'; |
| 697 | // The detached daemon has no PPID watchdog or stdin lifeline, so a |
| 698 | // wedged main thread would pin a core forever (#850). The liveness |
| 699 | // watchdog is its only recovery path. |
| 700 | this.livenessWatchdog = installMainThreadWatchdog(watchdogProgressPaths(root)); |
| 701 | return; // the net.Server keeps the process alive |
| 702 | } |
| 703 | |
| 704 | // Taken. If the holder is alive, another daemon already serves (or is |
| 705 | // binding) — we're redundant; exit cleanly so the launcher proxies to it. |
| 706 | const existing = lock.existing; |
| 707 | let disprovedLiveIdentity = false; |
| 708 | if (existing && existing.pid > 0 && isProcessAlive(existing.pid)) { |
| 709 | // Give a newly-elected daemon time to bind, then require its socket hello |
| 710 | // to match the lock PID/version. PID existence alone accepts an unrelated |
| 711 | // process after OS PID reuse and permanently wedges startup (#1553). |
| 712 | const age = Date.now() - existing.startedAt; |
| 713 | const startupGraceMs = 10_000; |
| 714 | const stillStarting = existing.startedAt > 0 && age >= 0 && age < startupGraceMs; |
| 715 | // Legacy plain-PID locks have no socket identity to test. Preserve those |
| 716 | // live holders: an inconclusive probe is not permission to create a |
| 717 | // second writer. |
| 718 | if ( |
| 719 | !canProbeDaemonIdentity(existing) || |
| 720 | stillStarting || |
| 721 | await probeDaemonIdentity(existing) |
| 722 | ) { |
| 723 | // A daemon the writer slot is held for outwaits a candidate still |
| 724 | // starting in its way, which cannot get the slot and leaves (#2335). |
| 725 | if (stillStarting && await outwaitCandidate(root, handoverFrom, existing.pid)) continue; |
| 726 | process.stderr.write( |
| 727 | `[CodeGraph daemon] Another daemon (pid ${existing.pid}) already holds the lock; exiting.\n` |
| 728 | ); |
| 729 | process.exit(0); |
no test coverage detected