daemon-pid-reuse.test.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // Preserve successful PID-reuse recovery alongside the live-lock guards in #1850.
  2. /**
  3. * Shared MCP daemon — issue #411.
  4. *
  5. * Validates the daemon architecture in `src/mcp/{daemon,proxy,session,index}.ts`
  6. * AFTER the review fixes:
  7. *
  8. * - The daemon is a *detached* background process; every `serve --mcp`
  9. * invocation is a thin proxy to it. Two invocations against one project
  10. * share ONE daemon.
  11. * - Concurrent launchers converge on a single daemon (the must-fix-1
  12. * lockfile-race: an empty-pidfile window used to let a racing candidate
  13. * delete the winner's lock → two daemons).
  14. * - Killing the launcher that spawned the daemon does NOT take the daemon
  15. * down — other attached clients keep working (the must-fix-2 detach: the
  16. * in-process daemon used to die with its launcher's process group and
  17. * orphan on host SIGKILL, regressing #277).
  18. * - A stale lockfile (dead pid) is cleared; `CODEGRAPH_NO_DAEMON=1` opts out;
  19. * the proxy refuses to attach across a version mismatch; the daemon
  20. * idle-times-out after the last client leaves (so a single session can't
  21. * leak a daemon forever).
  22. *
  23. * These tests intentionally spawn real `node dist/bin/codegraph.js` processes
  24. * over real sockets/pipes — the same surface a Claude Code / Cursor / Codex
  25. * install exercises. The daemon logs to `.codegraph/daemon.log` (it has no
  26. * client stderr of its own), so daemon-side assertions read that file.
  27. *
  28. * `realRoot` vs `tempDir`: processes are spawned with the (possibly symlinked)
  29. * `tempDir` as cwd/rootUri — on macOS `os.tmpdir()` lives under `/var`, a
  30. * symlink to `/private/var`, and a spawned child's `process.cwd()` is already
  31. * realpath'd. The daemon canonicalizes the root with `realpathSync`, so all
  32. * path assertions use `realRoot` (the canonical form). That this matches end to
  33. * end is itself the proof the canonicalization works.
  34. */
  35. import { afterEach, beforeEach, describe, expect, it } from 'vitest';
  36. import { ChildProcessWithoutNullStreams, spawn } from 'child_process';
  37. import * as fs from 'fs';
  38. import * as os from 'os';
  39. import * as path from 'path';
  40. import { CodeGraph } from '../src';
  41. import { getDaemonSocketPath } from '../src/mcp/daemon-paths';
  42. import { CodeGraphPackageVersion } from '../src/mcp/version';
  43. const BIN = path.resolve(__dirname, '../dist/bin/codegraph.js');
  44. interface SpawnedServer {
  45. child: ChildProcessWithoutNullStreams;
  46. stdout: string[];
  47. stderr: string[];
  48. }
  49. function spawnServer(cwd: string, env: NodeJS.ProcessEnv = {}): SpawnedServer {
  50. const child = spawn(process.execPath, [BIN, 'serve', '--mcp'], {
  51. cwd,
  52. stdio: ['pipe', 'pipe', 'pipe'],
  53. // #618: the daemon-attach log line is now off by default; opt the test
  54. // harness into it (CODEGRAPH_MCP_LOG_ATTACH=1) so the attach assertions
  55. // below can still observe a successful attach. A per-test env still wins.
  56. env: { CODEGRAPH_MCP_LOG_ATTACH: '1', ...process.env, ...env },
  57. }) as ChildProcessWithoutNullStreams;
  58. // Swallow spawn/EPIPE errors so killing a child mid-write can't surface as an
  59. // unhandled error that crashes the vitest worker.
  60. child.on('error', () => { /* ignore */ });
  61. child.stdin.on('error', () => { /* ignore */ });
  62. const stdout: string[] = [];
  63. const stderr: string[] = [];
  64. let stdoutBuf = '';
  65. let stderrBuf = '';
  66. child.stdout.on('data', (chunk: Buffer) => {
  67. stdoutBuf += chunk.toString('utf8');
  68. let idx: number;
  69. while ((idx = stdoutBuf.indexOf('\n')) !== -1) {
  70. stdout.push(stdoutBuf.slice(0, idx));
  71. stdoutBuf = stdoutBuf.slice(idx + 1);
  72. }
  73. });
  74. child.stderr.on('data', (chunk: Buffer) => {
  75. stderrBuf += chunk.toString('utf8');
  76. let idx: number;
  77. while ((idx = stderrBuf.indexOf('\n')) !== -1) {
  78. stderr.push(stderrBuf.slice(0, idx));
  79. stderrBuf = stderrBuf.slice(idx + 1);
  80. }
  81. });
  82. return { child, stdout, stderr };
  83. }
  84. function sendMessage(child: ChildProcessWithoutNullStreams, msg: unknown): void {
  85. try { child.stdin.write(JSON.stringify(msg) + '\n'); } catch { /* child may be gone */ }
  86. }
  87. function sendInitialize(child: ChildProcessWithoutNullStreams, rootUri: string, id: number): void {
  88. sendMessage(child, {
  89. jsonrpc: '2.0',
  90. id,
  91. method: 'initialize',
  92. params: {
  93. protocolVersion: '2024-11-05',
  94. capabilities: {},
  95. clientInfo: { name: 'test', version: '0.0.0' },
  96. rootUri,
  97. },
  98. });
  99. }
  100. /** Find a JSON-RPC response with the given id (result OR error) on stdout. */
  101. function findResponse(stdout: string[], id: number): any | null {
  102. for (const line of stdout) {
  103. if (!line.trim()) continue;
  104. try {
  105. const parsed = JSON.parse(line);
  106. if (parsed && parsed.id === id && (parsed.result !== undefined || parsed.error !== undefined)) {
  107. return parsed;
  108. }
  109. } catch { /* not JSON */ }
  110. }
  111. return null;
  112. }
  113. function waitFor<T>(
  114. predicate: () => T | undefined | null | false,
  115. timeoutMs: number,
  116. pollMs = 25,
  117. label = '',
  118. ): Promise<T> {
  119. return new Promise((resolve, reject) => {
  120. const started = Date.now();
  121. const tick = () => {
  122. let v: T | undefined | null | false;
  123. try { v = predicate(); } catch (e) { return reject(e); }
  124. if (v) return resolve(v as T);
  125. if (Date.now() - started > timeoutMs) {
  126. // Name the wait: an async stack loses the await site, so an unlabeled
  127. // timeout can't tell WHICH step flaked (the #662 test's recurring
  128. // timeout was undiagnosable for exactly this reason).
  129. return reject(new Error(`Timed out after ${timeoutMs}ms${label ? ` waiting for: ${label}` : ''}`));
  130. }
  131. setTimeout(tick, pollMs);
  132. };
  133. tick();
  134. });
  135. }
  136. function isAlive(pid: number): boolean {
  137. try { process.kill(pid, 0); return true; } catch { return false; }
  138. }
  139. function readLockPid(root: string): number | null {
  140. try {
  141. const raw = fs.readFileSync(path.join(root, '.codegraph', 'daemon.pid'), 'utf8');
  142. const info = JSON.parse(raw);
  143. return typeof info.pid === 'number' ? info.pid : null;
  144. } catch { return null; }
  145. }
  146. function readDaemonLog(root: string): string {
  147. try { return fs.readFileSync(path.join(root, '.codegraph', 'daemon.log'), 'utf8'); }
  148. catch { return ''; }
  149. }
  150. function countListeningLines(root: string): number {
  151. return readDaemonLog(root).split('\n').filter((l) => l.includes('[CodeGraph daemon] Listening on')).length;
  152. }
  153. function killTree(...procs: ChildProcessWithoutNullStreams[]): void {
  154. for (const p of procs) {
  155. if (!p.killed) { try { p.kill('SIGKILL'); } catch { /* gone */ } }
  156. }
  157. }
  158. async function waitProcessExit(pid: number, timeoutMs: number): Promise<boolean> {
  159. return waitFor(() => !isAlive(pid), timeoutMs).then(() => true).catch(() => false);
  160. }
  161. describe('Shared MCP daemon (issue #411)', () => {
  162. let tempDir: string; // the (possibly symlinked) path processes are spawned with
  163. let realRoot: string; // its canonical form — what the daemon keys paths on
  164. const servers: SpawnedServer[] = [];
  165. beforeEach(async () => {
  166. tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-mcp-daemon-'));
  167. const cg = await CodeGraph.init(tempDir);
  168. cg.close();
  169. realRoot = fs.realpathSync(tempDir);
  170. });
  171. afterEach(async () => {
  172. killTree(...servers.map((s) => s.child));
  173. // The daemon is detached (not a tracked child) — reap it explicitly via the
  174. // pid it recorded, so a test can't leak a background daemon. Guard against
  175. // our own pid: the version-mismatch test plants `pid: process.pid` in the
  176. // lockfile, and we must never SIGKILL the vitest worker.
  177. const daemonPid = readLockPid(realRoot);
  178. if (daemonPid && daemonPid !== process.pid && isAlive(daemonPid)) {
  179. try { process.kill(daemonPid, 'SIGKILL'); } catch { /* race */ }
  180. }
  181. await new Promise((r) => setTimeout(r, 50));
  182. servers.length = 0;
  183. fs.rmSync(tempDir, { recursive: true, force: true });
  184. });
  185. it('takes over after SIGKILL even when the stale PID has been reused (#1553)', async () => {
  186. const env = { CODEGRAPH_DAEMON_IDLE_TIMEOUT_MS: '30000' };
  187. const first = spawnServer(tempDir, env);
  188. servers.push(first);
  189. sendInitialize(first.child, `file://${tempDir}`, 1);
  190. await waitFor(() => findResponse(first.stdout, 1), 10000);
  191. await waitFor(() => countListeningLines(realRoot) >= 1, 10000);
  192. const killedPid = readLockPid(realRoot)!;
  193. process.kill(killedPid, 'SIGKILL');
  194. expect(await waitProcessExit(killedPid, 8000)).toBe(true);
  195. // Model OS PID reuse without risking another process: the stale lock now
  196. // names this live vitest worker, but no daemon answers the leftover socket.
  197. fs.writeFileSync(
  198. path.join(realRoot, '.codegraph', 'daemon.pid'),
  199. JSON.stringify({
  200. pid: process.pid,
  201. version: CodeGraphPackageVersion,
  202. socketPath: getDaemonSocketPath(realRoot),
  203. startedAt: Date.now() - 60_000,
  204. }),
  205. );
  206. const second = spawnServer(tempDir, env);
  207. servers.push(second);
  208. sendInitialize(second.child, `file://${tempDir}`, 2);
  209. const response = await waitFor(() => findResponse(second.stdout, 2), 12000);
  210. expect(response.result.serverInfo.name).toBe('codegraph');
  211. await waitFor(() => countListeningLines(realRoot) >= 2, 10000);
  212. const replacementPid = readLockPid(realRoot)!;
  213. expect(replacementPid).not.toBe(killedPid);
  214. expect(replacementPid).not.toBe(process.pid);
  215. expect(isAlive(replacementPid)).toBe(true);
  216. expect(isAlive(process.pid)).toBe(true);
  217. }, 50000);
  218. });