agent, wslrun: a wsl.exe that failed to say which distros run is asked again after 30s, not every 3s (#2341)

Both caches of `wsl.exe -l --running -q` (wslrun.running and agent's
wslRunningLocked) kept a failed answer as long as a real one: 3s. When
WSL hangs, each ask waits its whole 10s timeout, under a lock the GUI's
state build waits on (wslUp), and the timed-out wsl.exe is killed but the
Store WSL's own child isn't (CommandContext can't put it in a job), so
the lag came back every few seconds and wsl.exe processes piled up. A
failure is now kept upFailedAge / wslRunningFailedAge (30s). A failure
already reads as "none runs", so nothing is opened on a guess; a real
answer still lasts 3s, so a distro stopped is seen as quickly as before.

Run: go test -p 1 -tags nogui -count=1 ./internal/wslrun -run 'TestUp|TestFind'
and ./internal/agent -run TestWSL (both ok, macOS, sandbox HOME).
TestUpFailedKept and TestWSLRunningFailedKept count wsl.exe asks: a
failure isn't asked again after 3s, is after 30s, and an answer is asked
again after 3s.
Broken by hand: upFailedAge and wslRunningFailedAge set back to 3s ->
TestUpFailedKept and TestWSLRunningFailedKept fail ("a failure was asked
again after 3s: 2 asks"). GOOS=windows and GOOS=linux go vet -tags nogui
./internal/provider ./internal/agent ./internal/wslrun: clean; gofmt clean.
Not tried on Windows. The leftover wsl.exe children of a timed-out run
are not fixed here.

Full suite: SUITE_LINE
This commit is contained in:
yetone
2026-10-15 23:08:01 +08:00
parent 12d3db7332
commit fbbb9a8ffb
4 changed files with 104 additions and 5 deletions
+12 -2
View File
@@ -882,6 +882,7 @@ var wsl struct {
names []string // every distro installed
running map[string]bool // those running
runAt time.Time // when running was listed
runOK bool // running is wsl.exe's answer, not a failure
listed bool // names is a real answer, and may forget distros
seen map[string]*distro
probed map[string]bool
@@ -897,6 +898,10 @@ const (
// WSL) is seen stopped within it, before anything opens its files,
// which would start it again (TJHHHH on Discord)
wslRunningAge = 3 * time.Second
// wslRunningFailedAge is how long wsl.exe failing to say is kept: each
// ask that times out holds wsl's lock, and so the GUI, for 10s, and
// leaves a wsl.exe behind (#2341)
wslRunningFailedAge = 30 * time.Second
)
// wslOn is whether there is WSL to look in: on Windows, or in tests.
@@ -1035,10 +1040,15 @@ func wslForgetOldBins() {
// wsl.exe can't say, none is taken to run: a distro is never opened on a
// guess.
func wslRunningLocked() {
if !wsl.runAt.IsZero() && time.Since(wsl.runAt) <= wslRunningAge {
age := wslRunningAge
if !wsl.runOK {
age = wslRunningFailedAge
}
if !wsl.runAt.IsZero() && time.Since(wsl.runAt) <= age {
return
}
run, _ := wslList("-l", "--running", "-q")
run, ok := wslList("-l", "--running", "-q")
wsl.runOK = ok
wsl.running = map[string]bool{}
for _, n := range run {
wsl.running[n] = true
+41
View File
@@ -185,6 +185,7 @@ func fakeWSL(t *testing.T, installed, running string, probes, roots map[string]s
run, root := wslRun, wslRoot
reset := func() {
wsl.seen, wsl.at, wsl.runAt, wsl.names, wsl.running, wsl.dirty = nil, time.Time{}, time.Time{}, nil, nil, false
wsl.runOK = false
}
t.Cleanup(func() { wslRun, wslRoot = run, root; reset() })
reset()
@@ -317,6 +318,46 @@ func TestWSLListFails(t *testing.T) {
// (wsl --shutdown): its fields shown, the catalog synced, its check, the
// requests it made. None of them opens the stopped distro, which would
// start it: its fields read as last seen, and the next look neither probes
// #2341: wsl.exe failing to say which distros run (a hung WSL times out
// after 10s, under wsl's lock) is asked again only after
// wslRunningFailedAge, not every wslRunningAge; an answer still lasts only
// wslRunningAge.
func TestWSLRunningFailedKept(t *testing.T) {
fakeWSL(t, "Ubuntu\r\n", "", nil, nil)
asks := 0
var err error
wslRun = func(_ time.Duration, args ...string) ([]byte, error) {
if strings.Join(args, " ") != "-l --running -q" {
t.Fatalf("wsl.exe %v", args)
}
asks++
return utf16le("Ubuntu\r\n", true), err
}
aged := func(d time.Duration) {
wsl.Lock()
wsl.runAt = time.Now().Add(-d)
wsl.Unlock()
}
err = errors.New("timed out")
if wslUp("Ubuntu") || asks != 1 {
t.Fatalf("failed: up, %d asks", asks)
}
aged(wslRunningAge + time.Second)
if wslUp("Ubuntu") || asks != 1 {
t.Fatalf("a failure was asked again after %v: %d asks", wslRunningAge, asks)
}
aged(wslRunningFailedAge + time.Second)
err = nil
if !wslUp("Ubuntu") || asks != 2 {
t.Fatalf("after %v: %d asks", wslRunningFailedAge, asks)
}
aged(wslRunningAge + time.Second)
wslUp("Ubuntu")
if asks != 3 {
t.Fatalf("an answer was kept past %v: %d asks", wslRunningAge, asks)
}
}
// nor opens it.
func TestWSLStoppedSinceNotOpened(t *testing.T) {
codexHome(t, "", "")
+12 -3
View File
@@ -120,15 +120,24 @@ var up struct {
}
// upAge is how long running's answer is kept: a distro stopped is seen
// within it.
const upAge = 3 * time.Second
// within it. upFailedAge is how long a failed one is: wsl.exe that timed out
// is asked again only after it, as each ask can hold its caller (and the
// lock) the whole 10s and leaves a wsl.exe behind (#2341).
const (
upAge = 3 * time.Second
upFailedAge = 30 * time.Second
)
// running are the distros running, as wsl.exe -l --running says (which
// starts none) at most upAge ago; ok is false when it couldn't say.
func running() (names []string, ok bool) {
up.Lock()
defer up.Unlock()
if up.at.IsZero() || time.Since(up.at) > upAge {
age := upAge
if !up.ok {
age = upFailedAge
}
if up.at.IsZero() || time.Since(up.at) > age {
b, err := Run(10*time.Second, "-l", "--running", "-q")
up.names, up.ok, up.at = Distros(b), err == nil, time.Now()
if err != nil {
+39
View File
@@ -264,6 +264,45 @@ func TestUp(t *testing.T) {
}
}
// #2341: a wsl.exe that failed to say which distros run is asked again only
// after upFailedAge, not every upAge: each ask that times out holds the
// caller 10s and leaves a wsl.exe behind. An answer is still kept only upAge.
func TestUpFailedKept(t *testing.T) {
oldOn, oldRun := On, Run
t.Cleanup(func() { On, Run = oldOn, oldRun; Forget() })
On = true
Forget()
asks := 0
var err error
Run = func(_ time.Duration, args ...string) ([]byte, error) {
asks++
return utf16le("Ubuntu\r\n"), err
}
aged := func(d time.Duration) {
up.Lock()
up.at = time.Now().Add(-d)
up.Unlock()
}
err = errors.New("timed out")
if Up("Ubuntu") || asks != 1 {
t.Fatalf("failed: Up, %d asks", asks)
}
aged(upAge + time.Second)
if Up("Ubuntu") || asks != 1 {
t.Fatalf("a failure was asked again after %v: %d asks", upAge, asks)
}
aged(upFailedAge + time.Second)
err = nil
if !Up("Ubuntu") || asks != 2 {
t.Fatalf("after %v: %d asks", upFailedAge, asks)
}
aged(upAge + time.Second)
Up("Ubuntu")
if asks != 3 {
t.Fatalf("an answer was kept past %v: %d asks", upAge, asks)
}
}
// #1264: with Settings' Detect agents in WSL off, Find (Claude Code for a
// Claude subscription, when Windows has none) asks wsl.exe nothing.
func TestFindWithDetectionOff(t *testing.T) {