codegraph icon indicating copy to clipboard operation
codegraph copied to clipboard

Daemon watchdog SIGKILLs a healthy daemon during long syncs on any non-keyed root (multi-root progressPaths gap)

Open mradwankhalil opened this issue 1 month ago • 2 comments

Summary

In daemon mode, the liveness watchdog (#850) is armed with progressPaths covering only the daemon's keyed root (watchdogProgressPaths(root) → that root's codegraph.db + -wal). The shared daemon then multiplexes other project roots through the engine's setProjectLifecycle.open → acquireProject path, and watcher/catch-up syncs for those roots run cg.sync() on the main thread (reads are pooled, writes are not). A long sync on a non-keyed root therefore produces heartbeat silence with no disk progress on the watched (keyed) DB — and the watchdog SIGKILLs a perfectly healthy daemon at 60s. Clients retry, a fresh daemon re-attaches, re-syncs, and dies again: a crash loop.

Observed on codegraph 1.6.2, Windows 11, shared daemon (keyed on project A) serving a second churn-heavy root (project B) through an MCP gateway.

Evidence (from the live daemon log, 2026-10-07, UTC)

Every attach of the second root was followed by a kill within 24–70s:

22:59:59 File watcher active for …\gateways
23:00:52 File watcher active for …\tools        <- second root attaches
23:01:15 Main thread unresponsive for ~60s — killing the wedged process (#850)
23:10:10 File watcher active for …\tools
23:10:37 Main thread unresponsive for ~60s — killing …
23:55:35 File watcher active for …\tools
23:55:59 Main thread unresponsive for ~60s — killing …
00:01:15 File watcher active for …\tools
00:01:26 Main thread unresponsive for ~60s — killing …
00:16:23 File watcher active for …\tools
00:16:47 Main thread unresponsive for ~60s — killing …

The syncs making progress during those windows (Auto-synced 2 file(s) in 5431ms-scale stalls) were writing to the second root's DB — invisible to the watchdog, which only stats the keyed root's codegraph.db/-wal.

Root cause (code pointers, v1.6.2)

  • src/mcp/index.ts — daemon arm site: installMainThreadWatchdog(watchdogProgressPaths(root)) where watchdogProgressPaths returns exactly one root's DB + WAL. The child receives these as fixed argv paths.
  • src/mcp/daemon.ts — one MCPEngine({ queryPool: true }) multiplexes sessions for many roots.
  • src/mcp/engine.ts — setProjectLifecycle.open → acquireProject(root, …) lazily opens/watches arbitrary additional roots; catchUpSync() / watcher-triggered cg.sync() execute on the main thread.
  • src/mcp/liveness-watchdog.ts — the deferral added in #1231 only checks the fixed argv paths, so main-thread sync work on any other root's DB looks exactly like a wedge.

Second, related defect (same incident): cold attach scans throwaway test dirs on the main thread

The cold attach of the second root took >120s because the root contained 254 .pytest-* directories (36,136 subdirectories) under an agents-config-registry folder, walked synchronously on the daemon main thread (twice per attach). With a project-local codegraph.json {"exclude": [".pytest-*/"]} the same scan dropped from 11,150ms to 107ms and cold attach from >120s to 3.2s. Suggest (a) shipping default excludes for common throwaway test/temp dir patterns, and/or (b) moving the attach-time scan off the main thread — as-is, one large generated directory tree can keep any first query past typical client timeouts even when the watchdog is fixed.

Proposed fix (PR follows)

Make the watchdog's disk-progress deferral cover every root the daemon has attached, without changing the arm-time API:

  1. The watchdog child additionally stats paths listed in a small JSON file next to the keyed DB (<keyedRoot>/.codegraph/watchdog-paths.json), re-read on each check (argv paths remain authoritative for "deferral enabled").
  2. The daemon passes its keyed root to the engine (watchdogPathsRoot).
  3. The engine's project-open hook appends each opened root's codegraph.db + -wal to that file (idempotent, best-effort, try/catch everywhere).

Stale entries (deleted projects) stat-fail to a constant fingerprint, so they never create false deferrals; the hard cap (PROGRESS_CAP_MULTIPLIER) still bounds continuous-silence deferral exactly as before.

Local hot-patch of the above (against the installed 1.6.2 dist) plus the exclude config ended the loop: after the fix, the same second-root attach survived with zero watchdog kills, and three consecutive gateway-routed codegraph_explore calls answered in 40/37/50ms.

Alternatives considered: stop+re-arm the watchdog child with the union of paths on each attach (works; chattier, needs a daemon↔server reference); move all sync off the main thread (correct long-term, much larger change); widen the watchdog timeout (masks the symptom, leaves the wedge blind spot).

Cross-references

#850 (watchdog origin), #1231 (disk-progress deferral for CLI index), #1431 (kill-symptom tracker), #1887 (watchdog kills during bulk-load recovery — adjacent but distinct path), #905 (catch-up gate grace), #1740 (writer lock).

mradwankhalil avatar Oct 07 '26 02:10 mradwankhalil

Thanks for the detailed report and the hot-patch, @mradwankhalil. It pinpointed the gap exactly.

#2793 is merged to main. The background server's hang check now counts progress on every project index it keeps up to date. It does the same for the index a session updates itself while the server is unavailable. Once the server lets go of a project, it stops counting that project.

We took a slightly different route from #2406: the list of watched files lives in the process and is handed to a fresh watchdog whenever it changes, rather than kept in a file under .codegraph/. You're credited in the changelog.

The slow cold-attach scan of the .pytest-* trees isn't part of this fix, so your exclude workaround is still the way to go there. The fix ships in the next release.

colbymchenry avatar Oct 22 '26 09:10 colbymchenry

Thanks for fixing this and for the changelog credit. Understood that #2793 keeps the watched-file list in process and refreshes the watchdog as projects attach or detach, rather than using #2406's disk manifest. The separate .pytest-* cold-scan issue is not covered by this fix; I am not treating it as resolved or claiming a fresh retest of the merged change.

mradwankhalil avatar Oct 22 '26 13:10 mradwankhalil