Files
magpie/internal/proc/killtree_windows_test.go
T
yetone 68a96b67b8 proc: a command's context ending on Windows kills what it started too (#2341)
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.
2026-10-20 20:23:51 +08:00

77 lines
2.1 KiB
Go

package proc
import (
"context"
"testing"
"time"
"golang.org/x/sys/windows"
)
// alive says pid is a process still running.
func alive(pid uint32) bool {
h, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION, false, pid)
if err != nil {
return false
}
defer windows.CloseHandle(h)
var code uint32
return windows.GetExitCodeProcess(h, &code) == nil && code == 259 // STILL_ACTIVE
}
// The Store's WSL runs System32's wsl.exe, which starts Program Files'
// wsl.exe and waits on it. A listing past its timeout had only the first
// killed, and the second, waiting on a hung service, was left: dozens a day
// (#2341). A command's context ending kills what it started too. cmd.exe
// running ping stands in for the two wsl.exe.
func TestContextEndKillsWhatTheCommandStarted(t *testing.T) {
for _, name := range []string{"CommandContext", "ProbeContext"} {
t.Run(name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
mk := CommandContext
if name == "ProbeContext" {
mk = ProbeContext
}
cmd := mk(ctx, "cmd.exe", "/c", "ping -n 60 127.0.0.1 >NUL")
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
done := make(chan struct{})
go func() { cmd.Wait(); close(done) }()
var kids []uint32
for range 100 {
if kids = descendants(uint32(cmd.Process.Pid)); len(kids) > 0 {
break
}
time.Sleep(50 * time.Millisecond)
}
if len(kids) == 0 {
t.Fatal("cmd.exe started no ping")
}
t.Cleanup(func() {
for _, k := range kids {
if h, err := windows.OpenProcess(windows.PROCESS_TERMINATE, false, k); err == nil {
windows.TerminateProcess(h, 1)
windows.CloseHandle(h)
}
}
})
cancel()
select {
case <-done:
case <-time.After(10 * time.Second):
t.Fatal("cmd.exe not ended 10s after its context")
}
for _, k := range kids {
for i := 0; alive(k) && i < 40; i++ {
time.Sleep(50 * time.Millisecond)
}
if alive(k) {
t.Errorf("process %d that cmd.exe started still runs after its context ended", k)
}
}
})
}
}