Files
magpie/internal/agent/wsl_startup_test.go
T
yetone 6d24d4f4ea agent: a hung wsl.exe no longer holds every Windows command 20s
On a Windows box whose wsl.exe hangs (`wsl -l` there never answered),
every magpie command took ~20s, `magpie version` too. A goroutine dump
taken 8s in showed start-up's moves (RenameLegacy, then the Cursor and
Antigravity effort moves) walking All(), whose WSL agents list the
distros: `wsl -l -q` waited out its 10s timeout, then `wsl -l --running
-q` waited out another. `magpie providers` waited too, through Copilot's
sign-in looked for in each distro's home.

- Start-up's moves look at this machine's agents only (local()). They
  came before WSL agents (the dial rename 09-23, the effort moves 09-28,
  the day WSL Codex came), so a distro has nothing for them.
- A listing that failed isn't followed by the running list: no distro
  was listed, so none runs. That was a second timeout under wsl's lock,
  which the GUI waits on.
- A listing that hung (failed after 5s or more) is remembered in
  wsl-hung beside magpie's settings for 10 minutes: a new process
  doesn't list again in that time, and a listing that answers removes
  it. A process that runs on (the GUI) lists again each minute as
  before.

Real run on the Windows box (10.0.0.132), wsl.exe hung, sandbox
USERPROFILE, old build (main c89284ae6) against this one:
  version    20.17s -> 0.12s
  agents     20.29s -> 0.24s
  providers  20.21s -> 10.16s the first time (it records the hang),
             0.14s after
wsl-hung was written at the first run's hang.

Tests: TestStartUpMovesAskNoWSL (start-up's moves ask wsl.exe nothing,
All() still has the distro's agents), TestWSLListFailsAsksNoRunning,
TestWSLHangRememberedAcrossProcesses. The first two are red on the old
code; the third goes red with the hang check turned off by hand.

Run at this head (base b10f43e89): gofmt; GOOS=linux/windows/darwin go vet
-tags nogui ./internal/agent ./internal/cli; go test -count=1 -tags nogui
./internal/agent ./internal/cli: cli ok (38s), agent ok but one test:
TestWSLColdPlaceOpensNothing is red on main already (#3924, from
34d38ed66's Hermes profiles), with or without this change.

Not tried: a real WSL that answers slowly but under 10s; it lists as
before.
2026-10-28 00:36:15 +08:00

109 lines
3.0 KiB
Go

package agent
import (
"errors"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
// Every command ran start-up's moves over All(), which lists WSL's
// distros: on a Windows whose wsl.exe hangs, `magpie version` took 20s,
// two listings' timeouts. The moves look at this machine's agents only.
func TestStartUpMovesAskNoWSL(t *testing.T) {
syncHome(t)
t.Cleanup(FakeWSL(map[string]string{"Ubuntu": "home:/home/me\ndir:.codex\n"}, map[string]string{"Ubuntu": t.TempDir()}))
var asked []string
run := wslRun
wslRun = func(d time.Duration, args ...string) ([]byte, error) {
asked = append(asked, strings.Join(args, " "))
return run(d, args...)
}
RenameLegacy()
MoveCursorEfforts()
MoveAntigravityEfforts()
if len(asked) != 0 {
t.Fatalf("start-up asked wsl.exe %q", asked)
}
// All() still has the distro's agents
n := 0
for _, a := range All() {
if a.WSL != "" {
n++
}
}
if n == 0 || len(asked) == 0 {
t.Fatalf("All() found %d WSL agents, asked %q", n, asked)
}
}
// A wsl.exe that can't list the distros isn't asked which of them run:
// that is a second timeout, under the lock the GUI waits on.
func TestWSLListFailsAsksNoRunning(t *testing.T) {
fakeWSL(t, "", "", nil, nil)
var asked []string
wslRun = func(_ time.Duration, args ...string) ([]byte, error) {
asked = append(asked, strings.Join(args, " "))
return nil, errors.New("timed out")
}
wslDistros()
if len(asked) != 1 || asked[0] != "-l -q" {
t.Fatalf("asked %q", asked)
}
// and not again on the next look within the failed age
wslUp("Ubuntu")
if len(asked) != 1 {
t.Fatalf("asked %q", asked)
}
}
// A wsl.exe that hung to its timeout is remembered beside magpie's
// settings: the next command (a new process) doesn't wait on it again,
// and once a listing answers, it is forgotten.
func TestWSLHangRememberedAcrossProcesses(t *testing.T) {
fakeWSL(t, "", "", nil, nil)
after := wslHungAfter
wslHungAfter = 0
t.Cleanup(func() { wslHungAfter = after; os.Remove(wslHungPath()) })
os.MkdirAll(filepath.Dir(wslHungPath()), 0o755)
asks := 0
hung := true
wslRun = func(_ time.Duration, args ...string) ([]byte, error) {
asks++
if hung {
return nil, errors.New("timed out")
}
return []byte("Ubuntu\n"), nil
}
newProcess := func() {
wsl.Lock()
wsl.at, wsl.runAt, wsl.names, wsl.running = time.Time{}, time.Time{}, nil, nil
wsl.Unlock()
}
wslDistros()
if asks != 1 || !wslHungLately() {
t.Fatalf("asks %d, remembered %v", asks, wslHungLately())
}
newProcess()
wslDistros()
if asks != 1 {
t.Fatalf("a new process asked a hung wsl.exe again: %d", asks)
}
// it is asked again once the mark is old, and an answer clears it
old := time.Now().Add(-wslHungAge - time.Minute)
if err := os.Chtimes(wslHungPath(), old, old); err != nil {
t.Fatal(err)
}
hung = false
newProcess()
wslDistros()
if asks < 2 || wslHungLately() {
t.Fatalf("asks %d, remembered %v", asks, wslHungLately())
}
if _, err := os.Stat(wslHungPath()); !os.IsNotExist(err) {
t.Fatalf("mark kept: %v", err)
}
}