An old ~/.local/bin/cursor-agent that runs `cursor agent`, which (Cursor's own bin/cursor) first runs ~/.local/bin/cursor-agent --min-version=... status, calls itself without end. magpie asks cursor-agent who is signed in (about, 10s timeout) and kills the probe's session on the timeout with one SIGKILL to the group. On macOS that one signal can miss a process being forked as it goes out; the miss lives on, in the same group, adopted by init, and keeps calling itself until the user can start no process (JJ-Lin: ~4800 bash processes in one chain whose root's parent is PID 1). group.kill now sends SIGKILL to the group again every 5ms, behind the caller, until the group is gone (ESRCH) or 10s pass. The group keeps its id while any of it lives, so nothing else is signalled. It covers ProbeContext's timeout and EndProbes, and StartTree's Kill and sweep. Verified: - red without the change: TestProbeKillEndsWhatTheSignalMissed (the first group signal is made to miss, as the race does) fails with "what the probe started (pid 95576) outlived a kill that missed it once"; passes with it (-count=5). - reproduced the leak with the real Cursor 3.12.6 launcher script (/Applications/Cursor.app/.../bin/cursor, read only) and JJ-Lin's 87-byte wrapper in a sandbox HOME, under ulimit -u: ProbeContext(cursor-agent about) with a 10s timeout peaked at 311-700 processes; before the change 1 of 3 runs left a chain behind (root ppid 1, same pgid, args "agent --min-version=2025.10.01 status"), after it 0 of 6. A fake launcher (60 parallel self-calling chains, killed at 500ms) left survivors 4 of 30 runs before, 0 of 30 after. - the same through provider.askCursorStatus with the real launcher, 5 runs: no cursor process left after each. - go test -tags nogui ./internal/proc/ in a sandbox HOME; go vet -tags nogui of proc for darwin, linux and windows.
59 lines
1.9 KiB
Go
59 lines
1.9 KiB
Go
//go:build !windows
|
|
|
|
package proc
|
|
|
|
import (
|
|
"os/exec"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
// group is the session the command leads: its children are in it unless
|
|
// they make one of their own, and grok's MCP servers do, but those end when
|
|
// their input closes with the CLI.
|
|
type group struct{}
|
|
|
|
func ownGroup(cmd *exec.Cmd) {
|
|
if cmd.SysProcAttr == nil {
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{}
|
|
}
|
|
cmd.SysProcAttr.Setsid = true
|
|
}
|
|
|
|
func (group) join(*exec.Cmd) {}
|
|
|
|
// kill ends every process of the group. One signal to the group misses a
|
|
// process being forked as it is sent (macOS): a script that keeps starting
|
|
// others, as a launcher that calls itself does (Cursor's `cursor agent`
|
|
// running a ~/.local/bin/cursor-agent that runs `cursor agent`, #1278), had
|
|
// a child of the moment live on with its parent gone, adopted by init, and
|
|
// call itself until the user could start no process. What the signal missed
|
|
// is still in the group, and the group keeps its id while any of it lives,
|
|
// so the group is signalled again until none of it is left. Its leader,
|
|
// killed and not yet waited for, counts until it is: hence behind the
|
|
// caller, which waits for it next.
|
|
func (group) kill(cmd *exec.Cmd) {
|
|
pgid := cmd.Process.Pid
|
|
if signalGroup(pgid) != nil {
|
|
return // none of it is left
|
|
}
|
|
go func() {
|
|
for end := time.Now().Add(killFor); time.Now().Before(end); {
|
|
time.Sleep(5 * time.Millisecond)
|
|
if signalGroup(pgid) != nil {
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
// signalGroup kills the group pgid leads; a var so a test can have it miss.
|
|
var signalGroup = func(pgid int) error { return syscall.Kill(-pgid, syscall.SIGKILL) }
|
|
|
|
// killFor is how long a group is signalled again while some of it lives.
|
|
var killFor = 10 * time.Second
|
|
|
|
// sweep ends what is left of the group once its leader is gone: the group
|
|
// keeps the leader's pid while any of it runs, so none else has it.
|
|
func (g group) sweep(cmd *exec.Cmd) { g.kill(cmd) }
|