MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / startDaemonProcess

Method startDaemonProcess

src/mcp/index.ts:654–734  ·  view source on GitHub ↗

* 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

()

Source from the content-addressed store, hash-verified

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

Callers 1

startMethod · 0.95

Calls 15

startMethod · 0.95
resolveServerRootFunction · 0.90
tryAcquireDaemonLockFunction · 0.90
isProcessAliveFunction · 0.90
canProbeDaemonIdentityFunction · 0.90
probeDaemonIdentityFunction · 0.90
clearStaleDaemonLockFunction · 0.90
timestampStderrLinesFunction · 0.85
resolveDaemonRootFunction · 0.85
NumberInterface · 0.85

Tested by

no test coverage detected