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.
48 lines
1.7 KiB
Go
48 lines
1.7 KiB
Go
// Package proc makes the commands magpie runs: agents' CLIs, the probes
|
|
// that ask them what they have, the system's own tools. Every one is made
|
|
// here rather than with os/exec, so that on Windows none of them opens a
|
|
// window.
|
|
//
|
|
// The desktop app on Windows is linked as a GUI program and has no console.
|
|
// A console program it starts (devin, claude, codex, cursor-agent, grok, the
|
|
// probes behind them) is given a new console by Windows, and with it a
|
|
// window, one for every run; so a child of a magpie with no console is
|
|
// started with CREATE_NO_WINDOW, which gives it a console without one. A
|
|
// magpie run in a terminal, or the desktop app started from one with a
|
|
// command, lets its children share that console as before, so Ctrl+C there
|
|
// reaches them too. Nothing magpie starts reads or writes the terminal
|
|
// itself: its output is piped back, which a hidden console doesn't change.
|
|
package proc
|
|
|
|
import (
|
|
"context"
|
|
"os/exec"
|
|
"time"
|
|
)
|
|
|
|
// Command is exec.Command, with no window of its own on Windows.
|
|
func Command(name string, args ...string) *exec.Cmd {
|
|
cmd := exec.Command(name, args...)
|
|
batch(cmd)
|
|
hide(cmd)
|
|
return cmd
|
|
}
|
|
|
|
// CommandContext is exec.CommandContext, with no window of its own on
|
|
// Windows.
|
|
func CommandContext(ctx context.Context, name string, args ...string) *exec.Cmd {
|
|
cmd := exec.CommandContext(ctx, name, args...)
|
|
// the context ending kills the command, but not what it started: a CLI
|
|
// that is a script running node leaves node holding the output, and
|
|
// Output waited for it however long it took (#123) — its pipes are
|
|
// closed a moment after instead
|
|
cmd.WaitDelay = waitDelay
|
|
// on Windows what it started is killed with it (#2341)
|
|
cancelTree(cmd)
|
|
batch(cmd)
|
|
hide(cmd)
|
|
return cmd
|
|
}
|
|
|
|
var waitDelay = 2 * time.Second
|