Turning MCP servers off in the Library rewrote the agents' configs, but Codex, the Codex app and Claude Code read them only at start, so the servers they had started kept running. With every server turned off, the reporter's machine still held 17.75 GB of node processes for them. A change, and the sync at start, now record each stdio server it took out of an agent's config as that config had it. Once no agent's config runs that command and those arguments (another agent's entry, or the user's own under another name, keeps it), proc.EndTrees ends every process whose command line runs it, with all it started: the cmd /d /s /c "npx -y x" wrapper, the node npx-cli.js under it and the server under that. magpie and what it runs under are never picked, and a process under one picked goes with that one. An interpreter with nothing after it (node, npx) is never matched. It runs after the change lets go of the Library lock and before it returns, so magpie library mcp ends them too before it exits. On Windows the processes come from a Toolhelp snapshot with their command lines (no PowerShell); elsewhere from ps -o pid=,ppid=,args=. Run at basee54a7d50b, before the rebase onto34d38ed66: - gofmt -l internal/: clean; go vet -tags nogui for linux, windows, darwin on internal/proc, internal/library, internal/cli: ok - go test -count=1 -timeout 45m ./internal/proc ./internal/library ./internal/cli: ok (28s, 197s, 66s) - the proc test binary on the Windows box: TestParseFamily, TestPicks, TestEndTreesEndsWhatItStarted, TestEnd*, TestList*: PASS - broken by hand: endFamily ending the picked process alone on macOS made TestEndTreesEndsWhatItStarted red (its child ran 20s on); dropping the dropped-server record made TestDroppedServerEndedOnceNoAgentRunsIt red - real, on the Windows box in a sandbox USERPROFILE, with Node 22.11.0 (SHA-256 checked against nodejs.org's SHASUMS256): magpie library mcp add fs npx -y @modelcontextprotocol/server-filesystem <dir> agents=codex,claude; a stand-in agent started it as Codex does, through cmd with stdin held open: cmd -> node npx-cli.js -> cmd -> node index.js, 4 processes. Off for Codex only (Claude Code still has it): 4 left. Off for every agent: "ended 1 process trees of fs", 0 left, the stand-in agent still running. Not tried: a real Codex or Claude Code session holding the server (no signed-in agent may be copied to the sandbox). Not done here, from the same issue: writing the installed bin instead of npx, the opencode.json servers stub, and an applied.json record of MCP changes. At the rebased head (base34d38ed66): go build ./..., GOOS=linux/windows/darwin go vet -tags nogui ./internal/proc ./internal/library ok; TestParseFamily, TestPicks, TestEndTrees*, TestDroppedServer*, TestLaunchedBy ok. The rebase brought no proc or library changes.
109 lines
2.6 KiB
Go
109 lines
2.6 KiB
Go
package proc
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// A Kin is a running process with its parent's id, as a listing of every
|
|
// process saw it.
|
|
type Kin struct {
|
|
PID, PPID int
|
|
Args string
|
|
}
|
|
|
|
// EndTrees ends the processes match picks from every process this user
|
|
// can see, each with all it started, and says how many match picked. A
|
|
// process under one already picked goes with that one; magpie and what it
|
|
// runs under are never picked. It is how a turned-off MCP server's
|
|
// processes go when the agent that started them keeps running (#3885).
|
|
func EndTrees(ctx context.Context, match func(args string) bool) (int, error) {
|
|
ps, err := family(ctx)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
picked := picks(ps, match, os.Getpid())
|
|
for _, pid := range picked {
|
|
endFamily(pid, under(ps, pid))
|
|
}
|
|
return len(picked), nil
|
|
}
|
|
|
|
// parseFamily reads lines of "pid ppid command line", as ps prints them.
|
|
func parseFamily(out string) []Kin {
|
|
var ps []Kin
|
|
for _, line := range strings.Split(out, "\n") {
|
|
id, rest, _ := strings.Cut(strings.TrimSpace(line), " ")
|
|
parent, args, _ := strings.Cut(strings.TrimSpace(rest), " ")
|
|
pid, err1 := strconv.Atoi(id)
|
|
ppid, err2 := strconv.Atoi(parent)
|
|
if err1 != nil || err2 != nil || pid <= 0 {
|
|
continue
|
|
}
|
|
ps = append(ps, Kin{PID: pid, PPID: ppid, Args: strings.TrimSpace(args)})
|
|
}
|
|
return ps
|
|
}
|
|
|
|
// picks are the processes match picks, but those under another picked one
|
|
// and self with what it runs under.
|
|
func picks(ps []Kin, match func(string) bool, self int) []int {
|
|
parent := map[int]int{}
|
|
for _, p := range ps {
|
|
if p.PID != p.PPID {
|
|
parent[p.PID] = p.PPID
|
|
}
|
|
}
|
|
mine := map[int]bool{}
|
|
for p, n := self, 0; p > 0 && n < len(ps)+1; p, n = parent[p], n+1 {
|
|
mine[p] = true
|
|
}
|
|
hit := map[int]bool{}
|
|
for _, p := range ps {
|
|
if !mine[p.PID] && p.Args != "" && match(p.Args) {
|
|
hit[p.PID] = true
|
|
}
|
|
}
|
|
var out []int
|
|
for _, p := range ps {
|
|
if !hit[p.PID] {
|
|
continue
|
|
}
|
|
top := true
|
|
for q, n := parent[p.PID], 0; q > 0 && n < len(ps); q, n = parent[q], n+1 {
|
|
if hit[q] {
|
|
top = false
|
|
break
|
|
}
|
|
}
|
|
if top {
|
|
out = append(out, p.PID)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// under are the processes started under pid, at any depth, nearest first.
|
|
func under(ps []Kin, pid int) []int {
|
|
kids := map[int][]int{}
|
|
for _, p := range ps {
|
|
if p.PID != p.PPID {
|
|
kids[p.PPID] = append(kids[p.PPID], p.PID)
|
|
}
|
|
}
|
|
var out []int
|
|
seen := map[int]bool{pid: true}
|
|
for queue := []int{pid}; len(queue) > 0; queue = queue[1:] {
|
|
for _, k := range kids[queue[0]] {
|
|
if !seen[k] {
|
|
seen[k] = true
|
|
out = append(out, k)
|
|
queue = append(queue, k)
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|