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

Method start

src/mcp/daemon.ts:321–450  ·  view source on GitHub ↗

* Bind the socket, refresh the ownership record, kick off engine init, and * register signal handlers. The promise resolves once the server is listening * — the daemon then sticks around until idle/shutdown.

()

Source from the content-addressed store, hash-verified

319 * — the daemon then sticks around until idle/shutdown.
320 */
321 async start(): Promise<DaemonStartResult> {
322 // #1740: claim the project writer lock before opening/watching so a
323 // concurrent direct-mode serve --mcp cannot start a second watcher.
324 assertNoRebuild(this.projectRoot);
325 let writer = tryAcquireWriterLock(this.projectRoot, 'daemon');
326 // The launcher that stopped an older daemon has held the slot for us since
327 // (#2335); take it over without letting it fall free.
328 if (writer.kind === 'taken' && this.handoverFrom !== null &&
329 writer.existing?.pid === this.handoverFrom && writer.existing.mode === 'handover') {
330 const info: WriterLockInfo = { pid: process.pid, mode: 'daemon', startedAt: Date.now(), ready: false };
331 if (swapWriterLock(this.projectRoot, this.handoverFrom, info)) {
332 writer = { kind: 'acquired', pidPath: writer.pidPath, info };
333 process.stderr.write(`[CodeGraph daemon] Took over the writer lock from launcher pid ${this.handoverFrom}, which stopped an older daemon.\n`);
334 }
335 }
336 if (writer.kind === 'taken') {
337 const msg = writerLockHeldMessage(writer.existing, writer.pidPath);
338 process.stderr.write(`[CodeGraph daemon] ${msg}\n`);
339 this.cleanupLockfile();
340 throw new Error(msg);
341 }
342
343 let initialLockContents: string;
344 try {
345 initialLockContents = fs.readFileSync(this.pidPath, 'utf8');
346 if (decodeLockInfo(initialLockContents)?.pid !== process.pid) {
347 throw new Error('daemon lock belongs to another process');
348 }
349 } catch {
350 releaseWriterLock(this.projectRoot);
351 throw new Error('Lost daemon lock ownership before startup.');
352 }
353
354 // Walk the ordered socket candidates and bind the first that works. The
355 // in-project path comes first; the deterministic tmpdir path is the fallback
356 // for a filesystem that can't host an AF_UNIX node at all (ExFAT/FAT external
357 // volumes, some network mounts, WSL2 DrvFs → ENOTSUP/EACCES; #997, #974). The
358 // `listen` closure clears a stale socket (left by a SIGKILL'd previous daemon)
359 // before each attempt — safe because we hold the lockfile, so no live daemon
360 // owns it; without it `listen` would wedge on EADDRINUSE.
361 const candidates = getDaemonSocketCandidates(this.projectRoot);
362 const listen = (socketPath: string): Promise<net.Server> =>
363 new Promise<net.Server>((resolve, reject) => {
364 if (process.platform !== 'win32') {
365 try { fs.unlinkSync(socketPath); } catch { /* not-exists is fine */ }
366 }
367 const server = net.createServer((socket) => this.handleConnection(socket));
368 server.once('error', reject);
369 server.listen(socketPath, () => {
370 // POSIX: tighten permissions to user-only — the socket lives under
371 // `.codegraph/` (git-ignored, maybe a shared FS) or tmpdir.
372 if (process.platform !== 'win32') {
373 try { fs.chmodSync(socketPath, 0o600); } catch { /* best-effort */ }
374 }
375 resolve(server);
376 });
377 });
378

Callers 1

startDaemonProcessMethod · 0.95

Calls 15

cleanupLockfileMethod · 0.95
armIdleTimerMethod · 0.95
startLivenessTimersMethod · 0.95
stopMethod · 0.95
assertNoRebuildFunction · 0.90
tryAcquireWriterLockFunction · 0.90
swapWriterLockFunction · 0.90
writerLockHeldMessageFunction · 0.90
decodeLockInfoFunction · 0.90
releaseWriterLockFunction · 0.90
registerDaemonFunction · 0.90

Tested by

no test coverage detected