JJ-Lin's fault injection on #1278: when the first signal to a probe's group missed a process being forked, group.kill went on signalling it from a goroutine nobody waited for, and the probe counted as ended as soon as the first signal went out. EndProbes returned in ~0.2ms; magpie then exiting (or Exec'ing the new version) ended that goroutine, and the missed child lived on with PPID 1. group.kill now returns a channel closed once none of the group is left (or killFor has passed). A probe's Cancel marks its kill as begun and counts the probe ended only when that channel closes. EndProbes waits up to endWait for each kill to begin (a probe that already answered has none, as before) and then for the group to be empty, bounded by killFor plus a second. Every exit path goes through EndProbes: the GUI's OnShutdown, the CLI's return and signal handler, and update.Reexec. Windows: the job's TerminateJobObject takes no new process once it is ending, and a probe is killed alone with TerminateProcess, so there was no background loop to outlive; kill returns a closed channel there. The known Windows gap that a shim's children aren't in a job for probes is unchanged. Run: - TestEndProbesThenExitLeavesNothing (new) re-runs the test binary as a magpie whose group signals miss for 300ms, which calls EndProbes and os.Exit(0) at once. On the old code it fails 3/3 on darwin: "what the probe started (pid N) outlived magpie's exit after EndProbes". - darwin: go test -race ./internal/proc ok; -race -count=10 of the probe/EndProbes tests ok. - linux (docker golang:1.26.8 --init, GOTOOLCHAIN=local): -race -count=5 of the probe/tree tests ok. Without --init the container's PID 1 reaps nothing and every group test fails on zombies, on old code too. - Windows box: new TestEndProbesWindows (ping probe ended, EndProbes ~0ms) passes 3/3; the rest of the package passes except TestNoCommandBypassesProc, which walks ../.. and found other checkouts in C:\Users\yeton since the binary ran outside a tree. - gofmt -l, go vet (darwin), GOOS=linux vet -tags nogui, GOOS=windows vet.
51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package proc
|
|
|
|
import (
|
|
"os/exec"
|
|
"sync"
|
|
)
|
|
|
|
// A Tree is a started command and the processes it starts in turn: an
|
|
// agent's CLI with the MCP servers, shells and helpers it runs. Killing the
|
|
// CLI alone leaves those running, so they are ended with it.
|
|
type Tree struct {
|
|
cmd *exec.Cmd
|
|
|
|
mu sync.Mutex
|
|
done bool // waited for: its pid may be another process's now
|
|
group
|
|
}
|
|
|
|
// StartTree starts cmd in a group of its own: a session on Unix, a job on
|
|
// Windows. It has no terminal then, and a Ctrl+C there doesn't reach it.
|
|
func StartTree(cmd *exec.Cmd) (*Tree, error) {
|
|
ownGroup(cmd)
|
|
if err := cmd.Start(); err != nil {
|
|
return nil, err
|
|
}
|
|
t := &Tree{cmd: cmd}
|
|
t.join(cmd)
|
|
return t, nil
|
|
}
|
|
|
|
// Wait waits for the command, then ends what it left running.
|
|
func (t *Tree) Wait() error {
|
|
err := t.cmd.Wait()
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
t.done = true
|
|
t.sweep(t.cmd)
|
|
return err
|
|
}
|
|
|
|
// Kill ends the command and every process of its tree.
|
|
func (t *Tree) Kill() {
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
if t.done {
|
|
return
|
|
}
|
|
_ = t.kill(t.cmd)
|
|
_ = t.cmd.Process.Kill()
|
|
}
|