sessions: opening Sessions waits on no WSL listing it already has (shangyuan0818 on X)

shangyuan0818 (X, third round, after v0.1.4091 and v0.1.4255): on Windows,
Usage -> Sessions opened only after a fixed ~20s when sessions were long.
The long sessions were not the cause. Goroutine dumps of the reporter's
case on the Windows box showed four waits on WSL on the page's first asks:

- agent.Looks -> Clients -> All -> wslDistros: a blocking wslRefresh,
  `wsl -l -q` (10s timeout) once its list is a minute old, then
  `wsl -l --running -q` (10s) every time. A WSL that answers slowly but
  answers is never marked hung, so each open after a minute idle waited
  for both: about 18s, every time. ClientsIn drops WSL's agents anyway,
  so Clients now reads only this machine's (local()).
- gui agentsHere (the web-mode own-sessions gate) called agent.All the
  same way. It now calls agent.AllKnown: this machine's agents and WSL's
  as last listed (wslDistrosWithin(0)), never waiting on wsl.exe.
- sessions.wslFilesAs waited wslFirstWait (3s) afresh on each read while
  the first listing was pending, so list, stats and overview each waited
  3s in turn. The 3s is now counted once, from when the first listing
  started.
- provider.copilotCLILogin waited copilotWSLWait (2s) on every re-read
  of a distro's Copilot sign-in, every copilotWSLAge (20s), though it
  had an answer. A re-read now answers the last read at once; only the
  first read waits.

Timed on Windows 11 (xiaomi-gpu), sandbox HOME with a 90 MB Claude JSONL
and a 32 MB Codex rollout, a fake wsl.exe whose -l -q answers "Ubuntu"
after 8s and whose --running hangs; /api/sessions list+stats+overview
together, opened 20s after start, then 70s apart:

  b0674b9ec (old):  0.39s, 18.11s, 18.12s
  this commit:      0.38s,  0.07s,  0.07s

With wsl.exe hanging outright and Sessions opened right after start:
old 10.3-10.5s, new 3s (the designed first wait, now waited once). With
no WSL: 0.4s on both.

Tests, each red on the old code by hand:
- TestClientsAskNoWSL (agent): with Clients back on All(), red (waited
  past 5s on the hung wsl.exe); with AllKnown on wslDistros(), red.
- TestWSLFirstWaitIsWaitedOnce (sessions): four reads with the first
  listing hung; with the wait counted per read, red at 2.007s.
- TestCopilotWSLHangHoldsNoRequest (provider): a re-read on a hung WSL
  with copilotWSLWait 10s must answer within 2s; with the kept answer
  disabled, red at 2.002s.

Run at this tree:
- mote: go test -timeout 45m ./... at dac5095c3 (base b0674b9ec): exit 0, 102 packages ok, no FAIL. Rebased onto
  2ec8e6ad0 after it, which changes README files only.
- mica: go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./... ok; gofmt -l clean.
- WSL (magpie-desktop): internal/agent, sessions, provider ok.
- Windows: internal/agent, sessions, provider, gui: 66 reds, 64 of them
  red at origin/main 2ec8e6ad0 too (#6068, #1827, #4115, #2808, #4446,
  #5957, #5343, #5344). The other two, TestHermesProfilesAreNotListedAsAgents
  and TestHermesProfilesFollowMagpieModel, call onMachine, not the code
  changed here, pass alone 3 of 3 on this tree, and follow other Hermes
  tests red at main in the package run (#1841: Windows tests share
  APPDATA).

Not tried: a real WSL distro that is slow (the fake wsl.exe stands in
for it), and the reporter's own machine.
This commit is contained in:
yetone
2026-11-17 00:21:23 +08:00
parent 2ec8e6ad0c
commit ac4a4cf849
7 changed files with 134 additions and 10 deletions
+19 -1
View File
@@ -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")
+51
View File
@@ -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()
}
+3 -2
View File
@@ -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.
+10 -1
View File
@@ -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)))
+5 -1
View File
@@ -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)
+15 -5
View File
@@ -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
+31
View File
@@ -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")
}
}