diff --git a/internal/agent/agents.go b/internal/agent/agents.go index cfe1de85e..4aa99a45c 100644 --- a/internal/agent/agents.go +++ b/internal/agent/agents.go @@ -226,7 +226,12 @@ var others = []*Agent{ // Clients is everyone whose requests the gateway knows by name: every // agent, detected or not, and the others. It is what the Usage and // Routing views draw a request's client with. -func Clients() []*Agent { return ClientsIn(All()) } +// +// It leaves WSL's distros unasked: their agents are never clients, and a +// wsl.exe that hangs held the Sessions and Usage pages' first open (Looks) +// and the gateway's naming of a request's agent for its timeouts, ~10s +// to 20s (shangyuan0818 on X). +func Clients() []*Agent { return ClientsIn(local()) } // ClientsIn is Clients among all, a list of All() the caller has built // already. @@ -249,6 +254,19 @@ func All() []*Agent { return append(own, pluggedAgents(home, own, nil)...) } +// AllKnown is All() with the agents in WSL's distros as they were last +// listed: it starts a listing but never waits on wsl.exe, for a page's +// path (whether the Sessions page reads this machine's sessions), where a +// hung wsl.exe held the first open for its timeouts (shangyuan0818 on X). +func AllKnown() []*Agent { + home, cfg := homes() + own := onMachine(home, cfg) + if wslLooks() { + own = append(own, wslAgentsOf(wslDistrosWithin(0))...) + } + return append(own, pluggedAgents(home, own, nil)...) +} + func homes() (home, cfg string) { home, _ = os.UserHomeDir() cfg = appdir.Getenv("XDG_CONFIG_HOME") diff --git a/internal/agent/wsl_hang_test.go b/internal/agent/wsl_hang_test.go index f86996079..ac95d2b89 100644 --- a/internal/agent/wsl_hang_test.go +++ b/internal/agent/wsl_hang_test.go @@ -151,3 +151,54 @@ func TestWSLRootHangFailsProbe(t *testing.T) { t.Fatalf("root %q", r) } } + +// shangyuan0818 on X: Usage → Sessions took a fixed ~20s to open on +// Windows. Its handlers name each row's agent through Looks(), which built +// Clients() from All(), and All() waited for WSL's distros to be listed: +// a hung wsl.exe held the first open for its timeouts. WSL's agents are +// never clients, so neither Clients() nor Looks() asks wsl.exe at all. +func TestClientsAskNoWSL(t *testing.T) { + syncHome(t) + t.Cleanup(FakeWSL(map[string]string{"Ubuntu": "home:/home/me\ndir:.codex\n"}, map[string]string{"Ubuntu": t.TempDir()})) + release := make(chan struct{}) + t.Cleanup(func() { close(release); wslSettle() }) + var mu sync.Mutex + var asked []string + run := wslRun + wslRun = func(d time.Duration, args ...string) ([]byte, error) { + mu.Lock() + asked = append(asked, strings.Join(args, " ")) + mu.Unlock() + <-release // wsl.exe hangs + return run(d, args...) + } + t.Cleanup(func() { wslRun = run }) + forgetLooks() + done := make(chan struct{}) + go func() { + defer close(done) + Clients() + Looks() + }() + select { + case <-done: + case <-time.After(5 * time.Second): + t.Fatal("Clients() waited on a hung wsl.exe") + } + mu.Lock() + defer mu.Unlock() + if len(asked) != 0 { + t.Fatalf("Clients() asked wsl.exe %q", asked) + } + mu.Unlock() + // whether this computer has agents (the Sessions page's own-sessions + // gate) has WSL's agents as last listed, without waiting + done = make(chan struct{}) + go func() { defer close(done); AllKnown() }() + select { + case <-done: + case <-time.After(5 * time.Second): + t.Fatal("AllKnown() waited on a hung wsl.exe") + } + mu.Lock() +} diff --git a/internal/gui/gatewaymode.go b/internal/gui/gatewaymode.go index ccf866808..1f909e3b9 100644 --- a/internal/gui/gatewaymode.go +++ b/internal/gui/gatewaymode.go @@ -58,8 +58,9 @@ func gatewayMode(web bool) (bool, string) { // agentsHere says whether an agent is found on this computer, stopping at // the first: each agent's check can read app bundles and ask Spotlight. -// A test's own. -var agentsHere = func() bool { return slices.ContainsFunc(agent.All(), (*agent.Agent).Detected) } +// WSL's agents are those last listed: a hung wsl.exe doesn't hold the +// Sessions page (sessions.Dirs asks this). A test's own. +var agentsHere = func() bool { return slices.ContainsFunc(agent.AllKnown(), (*agent.Agent).Detected) } // gatewayModeRoutes: POST /api/settings/gateway-mode {"mode": "on" | "off" // | ""} sets Settings' choice, "" leaving it to magpie again. diff --git a/internal/provider/copilot_cli.go b/internal/provider/copilot_cli.go index 6802b4548..bad4689fc 100644 --- a/internal/provider/copilot_cli.go +++ b/internal/provider/copilot_cli.go @@ -67,7 +67,8 @@ var CopilotWSLDirs func() []string var copilotWSLAge = 20 * time.Second // copilotWSLWait is how long a request waits for a distro's sign-in to -// be read again, from when that read started: one read at a time, outside +// be read the first time, from when that read started (a read again +// answers the last one meanwhile): one read at a time, outside // copilotWSL's lock, and a read WSL holds up answers the sign-in read // before. Held under the lock, a hung WSL queued every request behind // the first, and the gateway sent 504s after 30s (vsoullai on X, #6167). @@ -122,6 +123,14 @@ func copilotCLILogin() (copilotApp, bool) { close(done) }() } + if !c.at.IsZero() { + // what was read before is answered while it is read again: each + // read waiting for it held a page's first ask (the Sessions page's, + // on a slow WSL) copilotWSLWait every copilotWSLAge (shangyuan0818 + // on X) + defer c.Unlock() + return c.app, c.ok + } done, start := c.done, c.start c.Unlock() t := time.NewTimer(time.Until(start.Add(copilotWSLWait))) diff --git a/internal/provider/copilot_wsl_hang_test.go b/internal/provider/copilot_wsl_hang_test.go index 5cbd1ef0a..4cc8c1183 100644 --- a/internal/provider/copilot_wsl_hang_test.go +++ b/internal/provider/copilot_wsl_hang_test.go @@ -73,7 +73,11 @@ func TestCopilotWSLHangHoldsNoRequest(t *testing.T) { copilotWSL.at = time.Now().Add(-copilotWSLAge - time.Second) copilotWSL.Unlock() - bar := copilotWSLWait + 2*time.Second + // a read again answers the one before at once, without waiting + // copilotWSLWait for it: each wait held the Sessions page's first ask + // every copilotWSLAge on a slow WSL (shangyuan0818 on X) + copilotWSLWait = 10 * time.Second + bar := 2 * time.Second var wg sync.WaitGroup start := time.Now() errs := make(chan string, 13) diff --git a/internal/sessions/wsl.go b/internal/sessions/wsl.go index 6a09c89ca..eeb83030a 100644 --- a/internal/sessions/wsl.go +++ b/internal/sessions/wsl.go @@ -181,6 +181,7 @@ var wslSess struct { loaded bool lists map[string]*wslListing // by distro at time.Time // when the last listing started + first time.Time // when the first listing started: reads wait wslFirstWait from it, in all done chan struct{} // the listing under way, nil when none kept []byte // what sessions-wsl.json holds } @@ -188,8 +189,11 @@ var wslSess struct { var ( // wslRelist is how often the distros' files are listed again wslRelist = 30 * time.Second - // wslFirstWait is how long a read waits for the very first listing, - // when nothing was kept of one; after it the read goes on without + // wslFirstWait is how long reads wait for the very first listing, + // when nothing was kept of one, all told from when it started; after + // it they go on without. Each read waiting it afresh held the Sessions + // page's first open, whose list, stats and overview each read, ~10s + // on a hung wsl.exe (shangyuan0818 on X). wslFirstWait = 3 * time.Second ) @@ -226,18 +230,24 @@ func wslFilesAs(calls bool, agents ...string) []file { if wslSess.done == nil && time.Since(wslSess.at) > wslRelist { done := make(chan struct{}) wslSess.done, wslSess.at = done, time.Now() + if wslSess.first.IsZero() { + wslSess.first = wslSess.at + } go wslList(wslSess.gen, done) } wait := wslSess.done if len(wslSess.lists) > 0 { wait = nil // what was found before is read now; the new listing comes next time } + left := wslFirstWait - time.Since(wslSess.first) wslSess.Unlock() - if wait != nil { + if wait != nil && left > 0 { + t := time.NewTimer(left) select { case <-wait: - case <-time.After(wslFirstWait): + case <-t.C: } + t.Stop() } want := map[string]bool{} for _, a := range agents { @@ -480,7 +490,7 @@ func wslReset() { wslSess.Lock() defer wslSess.Unlock() wslSess.gen++ - wslSess.loaded, wslSess.lists, wslSess.at, wslSess.done, wslSess.kept = false, nil, time.Time{}, nil, nil + wslSess.loaded, wslSess.lists, wslSess.at, wslSess.first, wslSess.done, wslSess.kept = false, nil, time.Time{}, time.Time{}, nil, nil } // wslResume is the PowerShell line that resumes a session in a WSL distro diff --git a/internal/sessions/wsl_test.go b/internal/sessions/wsl_test.go index c7fff8c83..3dd17bef3 100644 --- a/internal/sessions/wsl_test.go +++ b/internal/sessions/wsl_test.go @@ -388,3 +388,34 @@ func TestWSLNoneWithoutHomes(t *testing.T) { t.Fatal(r) } } + +// shangyuan0818 on X: on a wsl.exe that hangs, the first listing of the +// distros never ends in time, and each read waited wslFirstWait for it +// afresh: the Sessions page's list, stats and overview read one after +// another, ~10s in all. The reads wait wslFirstWait all told. +func TestWSLFirstWaitIsWaitedOnce(t *testing.T) { + wslDistro(t) + release := make(chan struct{}) + homes := WSLHomes + WSLHomes = func() []WSLHome { <-release; return homes() } // wsl.exe hangs + t.Cleanup(func() { close(release); wslSettle() }) + was := wslFirstWait + wslFirstWait = 500 * time.Millisecond + t.Cleanup(func() { wslFirstWait = was }) + + start := time.Now() + for range 4 { + wslFiles("claude", "codex") + } + // four waits afresh are 2s; once is 0.5s + if d := time.Since(start); d > 1500*time.Millisecond { + t.Fatalf("four reads took %v on a hung WSL", d) + } + // once the listing ends, what it found is read + close(release) + release = make(chan struct{}) + wslSettle() + if len(wslFiles("claude")) == 0 { + t.Fatal("the distro's sessions weren't read once listed") + } +}