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.
28 lines
735 B
Go
28 lines
735 B
Go
//go:build !windows
|
|
|
|
package proc
|
|
|
|
import (
|
|
"os/exec"
|
|
"time"
|
|
)
|
|
|
|
// probeGroup has the probe lead a session, and its context ending kill the
|
|
// session's group: the script at its head and what that started. The probe
|
|
// counts as ended once none of the group is left, not once the first signal
|
|
// went out (#1278).
|
|
func probeGroup(cmd *exec.Cmd, p *probe) {
|
|
ownGroup(cmd)
|
|
cmd.Cancel = func() error {
|
|
p.kill()
|
|
var g group
|
|
gone := g.kill(cmd)
|
|
go func() { <-gone; p.end() }()
|
|
return cmd.Process.Kill()
|
|
}
|
|
}
|
|
|
|
// killWait is how long EndProbes waits, past a kill's start, for the group
|
|
// to be empty: as long as the kill goes on signalling it, and a moment more.
|
|
func killWait() time.Duration { return killFor + time.Second }
|