A read of the sessions (List, Stats, Manage, the agents' lists, Codex's threads) took dbReadMu, a plain mutex, for the whole call, parse included. The start's warm-up reads every session file with a quarter of the cores, so the first pages and API calls after a start waited for its whole read before they could even look at the cache. - dbReadMu becomes storeMu, an RWMutex: reads take it shared, and only the changes to the files (Delete, a Codex provider move, a Claude project relocation) take it alone. - A file another refresh is reading now is in `parsing`, with a channel closed when its parse is in the cache. A second read serves the parse it already has of such a file, and waits only for one it has never read; it never reads the same file twice at once. - Each parse is published as it finishes, not in checkpoint batches, so a waiting reader gets it at once. - The shared store handles (OpenCode, Hermes, Alma, Cursor) closed at the end of every read; with reads overlapping, one could close a handle another still used. They now live as long as their last user (useDBs). get() and the titles path closed them without the lock at all before. Tests (internal/sessions/refresh_shared_test.go), each broken by hand: - TestReadNotHeldByAnotherRead: the warm-up's parse of 12 grown files is held; List and Manage answer within 10s from the kept parses, each file is parsed once in all, and a later List parses none. With storeMu.Lock in List: red, "List waited for another read of the same files to end". - TestReadWaitsForAFirstReadUnderWay: a file never read is waited for, not served empty, and read once. Without the waits: red, "List answered without the files being read". Without the skip of a file being read: panic, close of nil channel. - TestStoresLiveAsLongAsTheirLastUser: with the release always closing: red, "closed while a read still used it". All three pass with -race. Measured on a sandbox instance (600k gateway calls, ~4,600 Claude Code and Codex sessions, 300 grown since the last start; 4 API calls asked 3.5s after the routes, when the warm-up starts), base1a3b92688against this branch, back to back. The machine's load average was 350-480 (nine agents running suites), so these timings are mostly CPU wait and vary by tens of seconds run to run: first answer (stats?days=30) base 91s / 81s new 50s / 79s manage?agent=claude base 112s / 95s new 83s / 96s The pages no longer queue behind the warm-up's lock, but at this load the answer is bound by the CPU its own parse gets, so no gain shows above the noise in the second pair. On an idle machine the held reads are what #2776 reports; the tests above show they are no longer held. Run on origin/main2ceff7370with all four #2776 commits' changes (this one and the three beside it), load average 291-568 (nine agents running suites), macOS: - go test -p 4 ./... -timeout 45m, 09:35-10:49: 62 packages ok, among them agent (2310s), gateway (1707s), provider (764s), usage (83s). Red: - internal/sessions: TestWSLOmpProfiles, TestWSLStoppedDistroKept, TestWSLStoppedSinceNotOpened, TestWSLNoneWithoutHomes, each "TempDir: mkdir ...: no space left on device" (the disk filled mid-run). After space was freed, ./internal/sessions alone: ok (139s). - internal/gui: TestDockJoinLeadsCommandTab, TestPanelCloseHidesIt, TestPanelJoinsFullScreenSpaces, TestPluginPageLoadsItsFilesInTheWindow, TestDockReopenKeepsTheWindowsSpace (each killed at 30s) and TestMainWindowCorners (86s): the real-window AppKit tests under load, already filed as #2645 and #1936. Rerun alone at load ~335: all pass, with TestSetAnswersWhileDevinAndProvidersHang (17s). - gofmt -l: none. go vet: darwin, GOOS=linux -tags nogui and GOOS=windows all exit 0. - No internal/gui/assets file changed, so the GUI Playwright suite was not run. - At this commit alone (2ceff7370+ this change): ./internal/sessions ok (82s), and its three new tests with -race ok.
172 lines
4.4 KiB
Go
172 lines
4.4 KiB
Go
package sessions
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// growSessions adds a line to each of moreSessions' files, so each has to
|
|
// be read again.
|
|
func growSessions(t *testing.T, claude string, n int) {
|
|
t.Helper()
|
|
dir := filepath.Join(claude, "projects", "-work-many")
|
|
for i := range n {
|
|
p := filepath.Join(dir, fmt.Sprintf("33333333-2222-3333-4444-%012d.jsonl", i))
|
|
f, err := os.OpenFile(p, os.O_APPEND|os.O_WRONLY, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f.WriteString(ccMsg(fmt.Sprintf("n%d", i), "2026-09-21T11:00:00.000Z", 100, 10))
|
|
f.Close()
|
|
}
|
|
}
|
|
|
|
// A page asked for while the start's warm-up reads the changed files is
|
|
// answered with the parses kept of them, not held until the warm-up has
|
|
// read them all (#2776: the warm-up held the page for its whole read).
|
|
func TestReadNotHeldByAnotherRead(t *testing.T) {
|
|
inZone(t, 0)
|
|
claude, _ := setup(t)
|
|
moreSessions(t, claude, 12)
|
|
if n := len(List(0)); n < 12 {
|
|
t.Fatalf("listed %d sessions", n)
|
|
}
|
|
growSessions(t, claude, 12)
|
|
|
|
hold, started := make(chan struct{}), make(chan struct{})
|
|
var once sync.Once
|
|
var parsed atomic.Int32
|
|
parseHook = func() {
|
|
parsed.Add(1)
|
|
once.Do(func() { close(started) })
|
|
<-hold
|
|
}
|
|
release := sync.OnceFunc(func() { close(hold) })
|
|
t.Cleanup(func() { release(); parseHook = nil })
|
|
|
|
warm := make(chan struct{})
|
|
go func() { defer close(warm); statsAt(0, statsNow) }()
|
|
<-started
|
|
|
|
listed := make(chan []Session, 1)
|
|
go func() { listed <- List(0) }()
|
|
select {
|
|
case got := <-listed:
|
|
if len(got) < 12 {
|
|
t.Fatalf("listed %d sessions while another read ran", len(got))
|
|
}
|
|
case <-time.After(10 * time.Second):
|
|
t.Fatal("List waited for another read of the same files to end")
|
|
}
|
|
managed := make(chan ManageView, 1)
|
|
go func() {
|
|
managed <- Manage(map[string]bool{"claude": true}, func([]AgentCount, map[string]map[string]bool) string { return "claude" })
|
|
}()
|
|
select {
|
|
case v := <-managed:
|
|
if len(v.Sessions) < 12 {
|
|
t.Fatalf("managed %d sessions while another read ran", len(v.Sessions))
|
|
}
|
|
case <-time.After(10 * time.Second):
|
|
t.Fatal("Manage waited for another read of the same files to end")
|
|
}
|
|
release()
|
|
<-warm
|
|
if n := parsed.Load(); n != 12 {
|
|
t.Fatalf("the 12 changed files were read %d times", n)
|
|
}
|
|
// and what the warm-up read is in, for the next page
|
|
parsed.Store(0)
|
|
List(0)
|
|
if n := parsed.Load(); n != 0 {
|
|
t.Fatalf("read %d files again after the warm-up had read them", n)
|
|
}
|
|
}
|
|
|
|
// A file never read before has no parse to serve: a page waits for the
|
|
// read already under way, rather than reading it a second time.
|
|
func TestReadWaitsForAFirstReadUnderWay(t *testing.T) {
|
|
inZone(t, 0)
|
|
claude, _ := setup(t)
|
|
moreSessions(t, claude, 12)
|
|
|
|
hold, started := make(chan struct{}), make(chan struct{})
|
|
var once sync.Once
|
|
var parsed atomic.Int32
|
|
parseHook = func() {
|
|
parsed.Add(1)
|
|
once.Do(func() { close(started) })
|
|
<-hold
|
|
}
|
|
release := sync.OnceFunc(func() { close(hold) })
|
|
t.Cleanup(func() { release(); parseHook = nil })
|
|
|
|
warm := make(chan struct{})
|
|
go func() { defer close(warm); statsAt(0, statsNow) }()
|
|
<-started
|
|
before := parsed.Load()
|
|
|
|
listed := make(chan []Session, 1)
|
|
go func() { listed <- List(0) }()
|
|
select {
|
|
case <-listed:
|
|
t.Fatal("List answered without the files being read")
|
|
case <-time.After(200 * time.Millisecond):
|
|
}
|
|
release()
|
|
var got []Session
|
|
select {
|
|
case got = <-listed:
|
|
case <-time.After(30 * time.Second):
|
|
t.Fatal("List never answered")
|
|
}
|
|
<-warm
|
|
if len(got) < 12 {
|
|
t.Fatalf("listed %d sessions", len(got))
|
|
}
|
|
total := before
|
|
mu.Lock()
|
|
n := len(cache)
|
|
mu.Unlock()
|
|
if p := parsed.Load(); p != int32(n) {
|
|
t.Fatalf("%d files read %d times (%d before the page asked)", n, p, total)
|
|
}
|
|
}
|
|
|
|
// The stores two reads share stay open until the last of them is done: a
|
|
// read that ends first must not close what the other still reads.
|
|
func TestStoresLiveAsLongAsTheirLastUser(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "store.db")
|
|
if err := os.WriteFile(path, nil, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mu.Lock()
|
|
first := useDBs()
|
|
db := openDB(path)
|
|
second := useDBs()
|
|
if openDB(path) != db {
|
|
mu.Unlock()
|
|
t.Fatal("a second read opened its own handle")
|
|
}
|
|
second()
|
|
mu.Unlock()
|
|
if db == nil {
|
|
t.Fatal("not opened")
|
|
}
|
|
if err := db.Ping(); err != nil {
|
|
t.Fatalf("closed while a read still used it: %v", err)
|
|
}
|
|
mu.Lock()
|
|
first()
|
|
open := dbs[path] != nil
|
|
mu.Unlock()
|
|
if open || db.Ping() == nil {
|
|
t.Fatal("left open after the last read")
|
|
}
|
|
}
|