Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

gui: what a Sessions or Usage poll threw away goes back once the page rests

yetone94a70f1a0dbf
diff --git a/internal/gui/api.go b/internal/gui/api.go
index 365e9a31a..556c64881 100644
--- a/internal/gui/api.go
+++ b/internal/gui/api.go
@@ -2391,6 +2391,8 @@ func held(h http.Handler) http.Handler {
// a page waits on the session files: the warm-up reading
// them reads at full speed meanwhile
defer sessions.Ask()()
+ // and what the read threw away goes back once the page rests
+ defer readingPage()()
}
if !(api && r.Method == http.MethodGet) {
// anything that may have written drops what was held, once it
diff --git a/internal/gui/page_memory.go b/internal/gui/page_memory.go
new file mode 100644
index 000000000..1df7aa942
--- /dev/null
+++ b/internal/gui/page_memory.go
@@ -0,0 +1,78 @@
+package gui
+
+import (
+ "runtime/debug"
+ "sync"
+ "time"
+)
+
+// A Sessions or Usage page's poll reads every session file's listing and
+// the request ledger again: ~330 MB allocated every 15 s to keep ~105 MB.
+// Go keeps the pages that freed, and its scavenger hands them back only
+// slowly, never while the next collection's goal covers them, so an open
+// window sat at ~340 MB in Activity Monitor between polls. The gateway
+// hands its requests' pages back once it rests (#3472), but the window's
+// reads aren't its requests. So once those reads have rested for
+// pageRest, what they left goes back at once, as the warm-up's does.
+
+// pageRest is how long the page reads rest before that: a poll's few
+// reads come within a second of one another.
+const pageRest = 3 * time.Second
+
+var pageReads struct {
+ sync.Mutex
+ n int // reads in flight
+ last time.Time // when the latest ended
+ due *time.Timer // the release to come, or nil
+ rest time.Duration
+ // debug.FreeOSMemory, or a test's count
+ release func()
+}
+
+// readingPage counts a page read until done is called, and has what it
+// left handed back once no read has been in flight for pageRest.
+func readingPage() (done func()) {
+ pageReads.Lock()
+ pageReads.n++
+ pageReads.Unlock()
+ return sync.OnceFunc(func() {
+ pageReads.Lock()
+ defer pageReads.Unlock()
+ pageReads.last = time.Now()
+ if pageReads.n--; pageReads.n == 0 && pageReads.due == nil {
+ pageReads.due = time.AfterFunc(pageRestFor(), releaseIfPagesRest)
+ }
+ })
+}
+
+// pageRestFor is pageRest, or a test's. Called under pageReads' lock.
+func pageRestFor() time.Duration {
+ if pageReads.rest > 0 {
+ return pageReads.rest
+ }
+ return pageRest
+}
+
+// releaseIfPagesRest hands memory back when no page read has been in
+// flight for pageRest. A read in flight arms it again when it ends, and a
+// rest not long enough yet waits out the rest of it.
+func releaseIfPagesRest() {
+ pageReads.Lock()
+ pageReads.due = nil
+ if pageReads.n != 0 {
+ pageReads.Unlock()
+ return
+ }
+ if wait := pageRestFor() - time.Since(pageReads.last); wait > 0 {
+ pageReads.due = time.AfterFunc(wait, releaseIfPagesRest)
+ pageReads.Unlock()
+ return
+ }
+ release := pageReads.release
+ pageReads.Unlock()
+ if release != nil {
+ release()
+ return
+ }
+ debug.FreeOSMemory()
+}
diff --git a/internal/gui/page_memory_test.go b/internal/gui/page_memory_test.go
new file mode 100644
index 000000000..8f990d54a
--- /dev/null
+++ b/internal/gui/page_memory_test.go
@@ -0,0 +1,103 @@
+package gui
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "testing"
+ "time"
+)
+
+// The pages a poll's reads left go back once no read is in flight: not
+// while one is, once when the last ends, and a rest a new read cut short
+// is waited out again.
+func TestPageReadsReleasedAtRest(t *testing.T) {
+ released := make(chan struct{}, 4)
+ pageReads.Lock()
+ pageReads.rest, pageReads.release = 20*time.Millisecond, func() { released <- struct{}{} }
+ pageReads.Unlock()
+ t.Cleanup(func() {
+ pageReads.Lock()
+ if pageReads.due != nil {
+ pageReads.due.Stop()
+ pageReads.due = nil
+ }
+ pageReads.rest, pageReads.release = 0, nil
+ pageReads.Unlock()
+ })
+
+ a, b := readingPage(), readingPage()
+ a()
+ // one is still in flight
+ releaseIfPagesRest()
+ select {
+ case <-released:
+ t.Fatal("released while a read was in flight")
+ default:
+ }
+ b()
+ b() // done twice ends it once
+ select {
+ case <-released:
+ case <-time.After(10 * time.Second):
+ t.Fatal("not released once every read ended")
+ }
+
+ // a rest not long enough yet: the release waits out the rest of it
+ stopDue := func() {
+ if pageReads.due != nil {
+ pageReads.due.Stop()
+ pageReads.due = nil
+ }
+ }
+ pageReads.Lock()
+ pageReads.rest = time.Hour
+ pageReads.Unlock()
+ readingPage()()
+ pageReads.Lock()
+ stopDue()
+ pageReads.Unlock()
+ releaseIfPagesRest()
+ pageReads.Lock()
+ waiting := pageReads.due != nil
+ stopDue()
+ pageReads.last = time.Time{}
+ pageReads.Unlock()
+ if !waiting {
+ t.Error("a rest cut short isn't waited out again")
+ }
+ select {
+ case <-released:
+ t.Fatal("released before the reads rested")
+ default:
+ }
+ releaseIfPagesRest()
+ select {
+ case <-released:
+ default:
+ t.Fatal("not released once the reads rested")
+ }
+}
+
+// held counts the Sessions and Usage pages' reads, and no other request.
+func TestHeldCountsPageReads(t *testing.T) {
+ in := -1
+ h := held(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
+ pageReads.Lock()
+ in = pageReads.n
+ pageReads.Unlock()
+ }))
+ t.Cleanup(func() {
+ pageReads.Lock()
+ if pageReads.due != nil {
+ pageReads.due.Stop()
+ pageReads.due = nil
+ }
+ pageReads.Unlock()
+ })
+ for path, want := range map[string]int{"/api/sessions": 1, "/api/usage/requests": 1, "/api/state": 0} {
+ h.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, path, nil))
+ if in != want {
+ t.Errorf("%s: %d page reads in flight while it was answered, want %d", path, in, want)
+ }
+ }
+}