Skip to content

macOS: codegraph 进程在 Reasonix 退出后残留,导致系统卡顿 #3734

@ttmouse

Description

@ttmouse

问题描述

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.goTrackTree() + Job Object (JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE) 正确杀死了整个进程树。

修复方案

代码库中已有可复用的模式:internal/tool/builtin/bash_kill_other.go 使用 Setpgid + 负 PID 的 syscall.Kill 来杀死整个进程组。

需要在以下位置应用相同模式:

  1. internal/plugin/transport_stdio.go — 在 newStdioTransport() 中,启动子进程前设置 cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true},使其成为新进程组的 leader
  2. 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 机制正常工作)

Metadata

Metadata

Assignees

No one assigned

    Labels

    crashCrash / hang / freezelinuxLinux-specificmacosmacOS-specificmcpMCP servers / plugins (internal/plugin, codegraph)

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions