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.
60 lines
1.7 KiB
Go
60 lines
1.7 KiB
Go
package proc
|
|
|
|
import (
|
|
"os/exec"
|
|
"unsafe"
|
|
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
// group is a job the command is put in, and every process it starts with
|
|
// it; closing the job ends them all, as it does when magpie exits. A
|
|
// command that can't be put in one is killed alone.
|
|
type group struct{ job windows.Handle }
|
|
|
|
func ownGroup(*exec.Cmd) {}
|
|
|
|
func (g *group) join(cmd *exec.Cmd) {
|
|
job, err := windows.CreateJobObject(nil, nil)
|
|
if err != nil {
|
|
return
|
|
}
|
|
info := windows.JOBOBJECT_EXTENDED_LIMIT_INFORMATION{BasicLimitInformation: windows.JOBOBJECT_BASIC_LIMIT_INFORMATION{
|
|
LimitFlags: windows.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE}}
|
|
if _, err := windows.SetInformationJobObject(job, windows.JobObjectExtendedLimitInformation,
|
|
uintptr(unsafe.Pointer(&info)), uint32(unsafe.Sizeof(info))); err != nil {
|
|
_ = windows.CloseHandle(job)
|
|
return
|
|
}
|
|
h, err := windows.OpenProcess(windows.PROCESS_SET_QUOTA|windows.PROCESS_TERMINATE, false, uint32(cmd.Process.Pid))
|
|
if err != nil {
|
|
_ = windows.CloseHandle(job)
|
|
return
|
|
}
|
|
defer windows.CloseHandle(h)
|
|
if err := windows.AssignProcessToJobObject(job, h); err != nil {
|
|
_ = windows.CloseHandle(job)
|
|
return
|
|
}
|
|
g.job = job
|
|
}
|
|
|
|
// kill ends the job's processes. Unlike a Unix group, a job takes no new
|
|
// process once it is being ended, so nothing can be missed and nothing is
|
|
// left to wait on: the channel is closed already.
|
|
func (g *group) kill(*exec.Cmd) <-chan struct{} {
|
|
if g.job != 0 {
|
|
_ = windows.TerminateJobObject(g.job, 1)
|
|
}
|
|
return closed
|
|
}
|
|
|
|
var closed = func() chan struct{} { c := make(chan struct{}); close(c); return c }()
|
|
|
|
func (g *group) sweep(*exec.Cmd) {
|
|
if g.job != 0 {
|
|
_ = windows.CloseHandle(g.job)
|
|
g.job = 0
|
|
}
|
|
}
|