问题描述
Reasonix 退出后,codegraph 的子进程仍然残留在系统中。随着使用次数积累,40+ 个 codegraph 僵尸进程持续运行,导致:
- 电脑非常卡顿
- 新建对话需要等待很久才能响应
- 手动 kill 这些进程后恢复正常
根因分析
在 macOS (Unix) 上,internal/proc/kill_other.go 中的 KillTree() 只调用 cmd.Process.Kill(),这只杀死直接子进程(即 codegraph serve --mcp 进程)。
但是 CodeGraph 进程自身会派生子进程(工作进程、守护进程等),当父进程被杀死后,这些子进程成为孤儿进程继续运行。
对比之下,Windows 上通过 internal/proc/kill_windows.go 的 TrackTree() + Job Object (JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE) 正确杀死了整个进程树。
修复方案
代码库中已有可复用的模式:internal/tool/builtin/bash_kill_other.go 使用 Setpgid + 负 PID 的 syscall.Kill 来杀死整个进程组。
需要在以下位置应用相同模式:
internal/plugin/transport_stdio.go — 在 newStdioTransport() 中,启动子进程前设置 cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true},使其成为新进程组的 leader
internal/proc/kill_other.go — 将 KillTree() 从 cmd.Process.Kill() 改为 syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL),杀死整个进程组
这样当 Reasonix 退出时,codegraph 的进程树会被完整清理,不会留下孤儿进程。
受影响平台
- macOS ❌(有问题)
- Linux ❌(同样有问题,和 macOS 共用一个 build tag)
- Windows ✅(Job Object 机制正常工作)
问题描述
Reasonix 退出后,codegraph 的子进程仍然残留在系统中。随着使用次数积累,40+ 个 codegraph 僵尸进程持续运行,导致:
根因分析
在 macOS (Unix) 上,
internal/proc/kill_other.go中的KillTree()只调用cmd.Process.Kill(),这只杀死直接子进程(即codegraph serve --mcp进程)。但是 CodeGraph 进程自身会派生子进程(工作进程、守护进程等),当父进程被杀死后,这些子进程成为孤儿进程继续运行。
对比之下,Windows 上通过
internal/proc/kill_windows.go的TrackTree()+ Job Object (JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE) 正确杀死了整个进程树。修复方案
代码库中已有可复用的模式:
internal/tool/builtin/bash_kill_other.go使用Setpgid+ 负 PID 的syscall.Kill来杀死整个进程组。需要在以下位置应用相同模式:
internal/plugin/transport_stdio.go— 在newStdioTransport()中,启动子进程前设置cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true},使其成为新进程组的 leaderinternal/proc/kill_other.go— 将KillTree()从cmd.Process.Kill()改为syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL),杀死整个进程组这样当 Reasonix 退出时,codegraph 的进程树会被完整清理,不会留下孤儿进程。
受影响平台