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

Method stop

src/mcp/daemon.ts:463–509  ·  view source on GitHub ↗

Graceful shutdown: close all sessions, the engine, and clean up the lock.

(reason: string = 'stop')

Source from the content-addressed store, hash-verified

461
462 /** Graceful shutdown: close all sessions, the engine, and clean up the lock. */
463 async stop(reason: string = 'stop'): Promise<void> {
464 if (this.stopping) return;
465 this.stopping = true;
466 if (this.idleTimer) {
467 clearTimeout(this.idleTimer);
468 this.idleTimer = null;
469 }
470 if (this.maxIdleTimer) {
471 clearInterval(this.maxIdleTimer);
472 this.maxIdleTimer = null;
473 }
474 if (this.clientSweepTimer) {
475 clearInterval(this.clientSweepTimer);
476 this.clientSweepTimer = null;
477 }
478 if (this.installCheckTimer) {
479 clearInterval(this.installCheckTimer);
480 this.installCheckTimer = null;
481 }
482 if (this.rootCheckTimer) {
483 clearInterval(this.rootCheckTimer);
484 this.rootCheckTimer = null;
485 }
486 process.stderr.write(`[CodeGraph daemon] Shutting down (${reason}; clients=${this.clients.size}).\n`);
487 for (const session of [...this.clients]) {
488 try { session.stop(); } catch { /* best-effort */ }
489 }
490 this.clients.clear();
491 for (const socket of this.acceptedSockets) socket.destroy();
492 this.acceptedSockets.clear();
493 if (this.server) {
494 await new Promise<void>((resolve) => this.server!.close(() => resolve()));
495 this.server = null;
496 }
497 await this.engine.stop();
498 this.cleanupLockfile();
499 // Only this daemon's record: one that found another daemon serving its
500 // project leaves that one's record in place (#3245).
501 deregisterDaemon(this.projectRoot, process.pid);
502 if (process.platform !== 'win32') {
503 try { fs.unlinkSync(this.socketPath); } catch { /* may already be gone */ }
504 }
505 // POSIX exits here; Windows drains first (engine.stop() above began closing
506 // the file watcher, and exiting mid-teardown aborts the process). See
507 // finalizeDaemonExit / DAEMON_SHUTDOWN_BACKSTOP_MS.
508 finalizeDaemonExit(process.platform, (code) => process.exit(code));
509 }
510
511 private handleConnection(socket: net.Socket): void {
512 if (this.stopping) { socket.destroy(); return; }

Callers 5

startMethod · 0.95
armIdleTimerMethod · 0.95
startLivenessTimersMethod · 0.95
checkProjectMethod · 0.95
checkInstallMethod · 0.95

Calls 10

cleanupLockfileMethod · 0.95
deregisterDaemonFunction · 0.90
finalizeDaemonExitFunction · 0.85
exitMethod · 0.80
writeMethod · 0.65
stopMethod · 0.65
closeMethod · 0.65
resolveFunction · 0.50
clearMethod · 0.45
destroyMethod · 0.45

Tested by

no test coverage detected