The Store's WSL runs System32's wsl.exe, which starts Program Files'
wsl.exe and waits on it. When `wsl.exe -l --running -q` hung past its
timeout, CommandContext killed the first only; the second, waiting on
a hung WSL service, was left. With a retry every few seconds that came
to dozens of wsl.exe a day on the reporter's machine. ProbeContext had
the same gap on Windows: Output starts the command out of a job's
reach, so the probe was killed alone.
CommandContext's Cancel on Windows now finds the command's descendants
by parent pid (a Toolhelp snapshot; a reused pid is told apart by a
start before its parent's) and terminates them before the command.
ProbeContext's Windows cancel does the same. wslrun's cancelable kept
its own Cancel and dropped proc's: it now runs proc's after stopping
the run inside the distro, so it gets the tree kill too (and a probe's
end, which it had lost).
Measured on the Windows box (10.0.0.132), cmd.exe running ping as the
two wsl.exe:
before: 1 child left running per ended context (CommandContext and
ProbeContext both)
after: 0 left
Run:
- TestContextEndKillsWhatTheCommandStarted, windows/amd64 test binary
on the Windows box. With killDescendants commented out in both
cancels:
killtree_windows_test.go:71: process 57548 that cmd.exe started still runs after its context ended
killtree_windows_test.go:71: process 42264 that cmd.exe started still runs after its context ended
--- FAIL: TestContextEndKillsWhatTheCommandStarted (4.08s)
With the fix: --- PASS (0.09s), both subtests.
- The whole proc test binary on the Windows box: everything passes but
TestNoCommandBypassesProc, which walks the source tree from the
binary's folder and read other checkouts under C:\Users\yeton (the
binary ran outside a checkout). wslrun's test binary there: PASS.
- macOS, sandbox HOME: go test -count=1 -timeout 45m ./internal/proc/
./internal/wslrun/ -> ok proc 54.098s, ok wslrun 2.872s.
- gofmt -l clean; go vet ./internal/proc ./internal/wslrun;
GOOS=linux and GOOS=windows go vet -tags nogui ./... clean.
Not tried: a real hung WSL service. The box's WSL answers, so the
reporter's hang itself wasn't reproduced; the test's cmd.exe→ping
stands in for System32 wsl.exe→Program Files wsl.exe.
92 lines
2.5 KiB
Go
92 lines
2.5 KiB
Go
package proc
|
|
|
|
import (
|
|
"os/exec"
|
|
"unsafe"
|
|
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
// cancelTree has a command's context ending kill what the command started
|
|
// as well as the command. Output starts a command out of a job's reach (a
|
|
// job is joined only once it has started), so the descendants are found by
|
|
// their parent at the kill. The Store's WSL runs System32's wsl.exe, which
|
|
// starts Program Files' wsl.exe and waits on it: a listing that hung past
|
|
// its timeout had the first killed and the second left waiting on the
|
|
// service, one more for every retry, dozens a day (#2341).
|
|
func cancelTree(cmd *exec.Cmd) {
|
|
cmd.Cancel = func() error {
|
|
killDescendants(cmd.Process.Pid)
|
|
return cmd.Process.Kill()
|
|
}
|
|
}
|
|
|
|
// killDescendants ends every process started under pid, at any depth. A
|
|
// pid Windows has given again to a later process is told apart by its
|
|
// start: a child starts after its parent.
|
|
func killDescendants(pid int) {
|
|
for _, d := range descendants(uint32(pid)) {
|
|
h, err := windows.OpenProcess(windows.PROCESS_TERMINATE, false, d)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
_ = windows.TerminateProcess(h, 1)
|
|
_ = windows.CloseHandle(h)
|
|
}
|
|
}
|
|
|
|
// descendants lists the processes under pid, nearest first.
|
|
func descendants(pid uint32) []uint32 {
|
|
snap, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, 0)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
defer windows.CloseHandle(snap)
|
|
kids := map[uint32][]uint32{}
|
|
var e windows.ProcessEntry32
|
|
e.Size = uint32(unsafe.Sizeof(e))
|
|
for err = windows.Process32First(snap, &e); err == nil; err = windows.Process32Next(snap, &e) {
|
|
if e.ProcessID != e.ParentProcessID {
|
|
kids[e.ParentProcessID] = append(kids[e.ParentProcessID], e.ProcessID)
|
|
}
|
|
}
|
|
var out []uint32
|
|
seen := map[uint32]bool{pid: true}
|
|
queue := []uint32{pid}
|
|
for len(queue) > 0 {
|
|
p := queue[0]
|
|
queue = queue[1:]
|
|
born, ok := started(p)
|
|
if !ok {
|
|
continue
|
|
}
|
|
for _, k := range kids[p] {
|
|
if seen[k] {
|
|
continue
|
|
}
|
|
if t, ok := started(k); !ok || t < born {
|
|
// the parent's pid is another, older process's
|
|
continue
|
|
}
|
|
seen[k] = true
|
|
out = append(out, k)
|
|
queue = append(queue, k)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// started is when pid started, in 100ns since 1601.
|
|
func started(pid uint32) (int64, bool) {
|
|
h, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION, false, pid)
|
|
if err != nil {
|
|
return 0, false
|
|
}
|
|
defer windows.CloseHandle(h)
|
|
var c, x, k, u windows.Filetime
|
|
if err := windows.GetProcessTimes(h, &c, &x, &k, &u); err != nil {
|
|
return 0, false
|
|
}
|
|
return int64(c.HighDateTime)<<32 | int64(c.LowDateTime), true
|
|
}
|