1
0

daemon-bind-failure.test.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /**
  2. * Daemon bind-failure cleanup — issue #974.
  3. *
  4. * A detached daemon acquires the `.codegraph/daemon.pid` lock (via
  5. * `tryAcquireDaemonLock`) BEFORE it binds its socket. If the bind then fails —
  6. * e.g. AF_UNIX is unsupported/unreliable on the filesystem (the WSL2 DrvFs
  7. * hazard behind #974) — `Daemon.start()` must release that lockfile before it
  8. * propagates the error and exits. Otherwise the next launcher reads a stale lock
  9. * pointing at the now-dead pid and the process pileup the issue reported recurs.
  10. *
  11. * We force a deterministic bind failure by planting a *directory* at the socket
  12. * path: `unlinkSync` (the daemon's stale-socket clear) can't remove a directory,
  13. * so it survives and `listen()` fails with EADDRINUSE.
  14. */
  15. import { afterEach, describe, expect, it } from 'vitest';
  16. import * as fs from 'fs';
  17. import * as os from 'os';
  18. import * as path from 'path';
  19. import { Daemon, tryAcquireDaemonLock } from '../src/mcp/daemon';
  20. import { getDaemonPidPath, getDaemonSocketPath } from '../src/mcp/daemon-paths';
  21. const tmpRoots: string[] = [];
  22. afterEach(() => {
  23. while (tmpRoots.length) {
  24. const root = tmpRoots.pop()!;
  25. try { fs.rmSync(root, { recursive: true, force: true }); } catch { /* best-effort */ }
  26. }
  27. });
  28. describe('Daemon.start() bind failure (#974)', () => {
  29. it.runIf(process.platform !== 'win32')('releases the lockfile it acquired when the socket cannot bind', async () => {
  30. const root = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-bind-'));
  31. tmpRoots.push(root);
  32. // Acquire the lock exactly as the detached-daemon startup does.
  33. const lock = tryAcquireDaemonLock(root);
  34. expect(lock.kind).toBe('acquired');
  35. const pidPath = getDaemonPidPath(root);
  36. expect(fs.existsSync(pidPath)).toBe(true);
  37. // Make the socket path un-bindable: a directory can't be unlink'd by the
  38. // daemon's stale-socket clear, and listen() on it fails with EADDRINUSE.
  39. const sockPath = getDaemonSocketPath(root);
  40. fs.mkdirSync(sockPath, { recursive: true });
  41. // The tmpdir-fallback socket path can live outside `root`; clean it too.
  42. tmpRoots.push(sockPath);
  43. const daemon = new Daemon(root);
  44. await expect(daemon.start()).rejects.toThrow();
  45. // The lockfile must be gone so the next launcher doesn't spin on a stale lock.
  46. expect(fs.existsSync(pidPath)).toBe(false);
  47. });
  48. });