Skip to content

fix(mcp): daemon exits when its project root is deleted (fixes #3245) - #3259

Open
johnhldiver-beep wants to merge 1 commit into
colbymchenry:mainfrom
johnhldiver-beep:fix/3245-daemon-exits-when-root-deleted
Open

johnhldiver-beep wants to merge 1 commit into
colbymchenry:mainfrom
johnhldiver-beep:fix/3245-daemon-exits-when-root-deleted

Conversation

@johnhldiver-beep

Copy link
Copy Markdown

Fixes #3245.

Problem

A CodeGraph daemon kept running with its recursive fs.watch / FSEvents stream open after its project root (a git worktree) was deleted. It only exited when the last client disconnected and the 300s idle timeout had passed, leaving a stale registry entry and a watcher on a deleted tree.

Fix

  • The daemon now checks every 5 seconds whether its project root (and <root>/.codegraph) still exists. If either is gone it calls stop('project root deleted'): stops the watcher, removes its registry entry, and exits. The interval is overridable with CODEGRAPH_DAEMON_ROOT_CHECK_MS (0 disables the check). The timer is unref'd, so it cannot keep the process alive on its own.
  • Daemon.stop() now wraps engine.stop() in try/finally, so cleanupLockfile() and deregisterDaemon() always run even if engine.stop() throws.

Verification (all numbers from disk/logs, Linux)

  • Failing test first: the new regression tests in __tests__/daemon-root-deleted.test.ts fail 2/2 on unfixed code (daemon keeps running after root deletion) and pass 2/2 with the fix. The gate-1 check was redone by hand: stash daemon.ts + rebuild dist (verified by grep that dist contains no fix) → 2 failed; restore + rebuild → 2 passed.
  • tsc exit 0.
  • Full suite (npm test, 887 files, ~34 min): 867 passed | 1 failed | 19 skipped; 10964 tests: 10518 passed | 1 failed | 2 expected fail | 443 skipped.
  • Build (npm run build) succeeds in 6.01s, re-run after the last edit; dist contains the fix.

One failure, disclosed, proven unrelated

mcp-daemon.test.ts #1963: daemon exit times out within 1500ms after SIGTERM, in the full-suite run only. It is not a regression from this change:

  • The same test run alone passes 2/2; the whole file run alone passes 24/24.
  • The fix cannot slow a SIGTERM exit: rootCheckTimer is unref'd and only fires when the root is gone (it does not trigger while the root exists), and the try/finally in stop() takes the exact same cleanup path when engine.stop() does not throw.
  • Conclusion: timing jitter in the 1500ms window under full-suite parallel load.

…#3245)

The daemon kept running with its recursive watcher open after its
project root (a git worktree) was deleted, only exiting when the last
client disconnected and the 300s idle timeout passed. It now checks
every 5s (CODEGRAPH_DAEMON_ROOT_CHECK_MS, 0 disables) and stops
promptly, removing its registry entry and stopping its watcher.
Daemon.stop() also wraps engine.stop() in try/finally so the lockfile
cleanup and deregistration always run.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Daemon keeps running and keeps its recursive watcher after its project root is deleted

1 participant