fix(codegraph): shorten shared daemon idle cleanup safely / 安全缩短共享 daemon 空闲清理#4332
Merged
Merged
Conversation
CodeGraph serve --mcp starts a daemon that calls setsid(2) to detach into its own process group. On Unix, KillTree sends SIGKILL to the launcher's process group (syscall.Kill(-pid, SIGKILL)) — but the daemon has already moved to a new group, so it survives and runs until its 5-minute idle timeout fires. (Windows is unaffected: the Job Object with KILL_ON_JOB_CLOSE captures all descendants regardless of reparenting.) Fix: - codegraph.DaemonPID(root) parses the last daemon PID from .codegraph/daemon.log - codegraph.KillDaemon(root) sends a direct SIGKILL to that PID - boot.Build chains KillDaemon into the cleanup closure so every ctrl.Close() path — normal exit, model switch, tab rebuild, desktop shutdown — reaps the escaped daemon - Both E2E tests now clean up their daemons; a new E2E test (TestE2ECodegraphKillDaemon) proves the daemon survives process-group kill and dies to the explicit KillDaemon call
Use CodeGraph’s idle-timeout knob instead of killing the root-scoped daemon from controller cleanup, so active clients on the same project keep their shared daemon alive. Harden explicit daemon cleanup by reading the structured daemon.pid lockfile and requiring a matching socket hello before sending a direct kill, preventing stale log/PID reuse from killing unrelated processes.
Collaborator
|
Follow-up hardening pushed in What changed:
Verified:
|
Collaborator
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: edd4996fc3
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
esengine
pushed a commit
that referenced
this pull request
Jun 14, 2026
…4363) Extract a shared codegraph.MCPSpec helper so the built-in CodeGraph MCP spec (daemon idle-timeout env, codegraph_ prefix strip, low-priority, read-only tool names) stays identical across normal boot, token-economy on-demand enablement, and manual MCP reconnect. The on-demand and reconnect paths previously drifted and omitted the idle-timeout env (and prefix/priority), which this centralizes. Follow-up to #4332.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
CodeGraph
serve --mcpstarts a root-scoped shared daemon that detaches into its own process group. The launcher/proxy can be killed by Reasonix cleanup, but the daemon intentionally outlives any single MCP client and exits only after its idle timer once the last client disconnects.This PR keeps that shared-client lifecycle intact while making Reasonix-launched daemons short-lived after use.
Changes:
CODEGRAPH_DAEMON_IDLE_TIMEOUT_MS=1000on the built-in CodeGraph MCP spec, so a daemon started by Reasonix exits quickly after the last client disconnects instead of lingering for the default 5 minutes.KillDaemon(root)from normalctrl.Close()cleanup. Closing a controller now only tears down Reasonix’s proxy connection; CodeGraph’s own refcount decides when the shared daemon may exit.KillDaemon(root)for explicit test/diagnostic cleanup, but harden it to read.codegraph/daemon.pidand require the daemon socket hello to match the recorded pid/socket/protocol before sending a direct kill. This avoids killing an unrelated process after stale PID reuse.Verification:
go test ./internal/codegraphgo test ./internal/bootgit diff --checkAuthorship note: @heshuimu is the primary author of the original PR. @SivanCola contributed the follow-up hardening commit that preserves CodeGraph shared-daemon semantics and guards explicit daemon cleanup against stale PID reuse.