diff --git a/internal/agent/wsl.go b/internal/agent/wsl.go index edcf82397..a042de471 100644 --- a/internal/agent/wsl.go +++ b/internal/agent/wsl.go @@ -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 diff --git a/internal/agent/wsl_test.go b/internal/agent/wsl_test.go index db0a862bc..1852df423 100644 --- a/internal/agent/wsl_test.go +++ b/internal/agent/wsl_test.go @@ -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, "", "") diff --git a/internal/wslrun/wslrun.go b/internal/wslrun/wslrun.go index 7c3a71790..221c6d0c1 100644 --- a/internal/wslrun/wslrun.go +++ b/internal/wslrun/wslrun.go @@ -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 { diff --git a/internal/wslrun/wslrun_test.go b/internal/wslrun/wslrun_test.go index 2399db73e..e083473e5 100644 --- a/internal/wslrun/wslrun_test.go +++ b/internal/wslrun/wslrun_test.go @@ -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) {