diff --git a/internal/sessions/codex.go b/internal/sessions/codex.go index 50730fc22..8d27e7bc0 100644 --- a/internal/sessions/codex.go +++ b/internal/sessions/codex.go @@ -277,12 +277,12 @@ func noteCodexIdentity(s *state, identity CodexIdentity, conflict bool) { // CodexIdentities returns an independent snapshot of the native thread identities. func CodexIdentities() map[string]CodexIdentity { - dbReadMu.Lock() - defer dbReadMu.Unlock() + storeMu.RLock() + defer storeMu.RUnlock() mu.Lock() defer mu.Unlock() loadCache() - defer closeDBs() + defer useDBs()() want := append(codexFiles(), wslFiles("codex")...) refresh(want, nil) return codexIdentitySnapshot(want) diff --git a/internal/sessions/codex_provider.go b/internal/sessions/codex_provider.go index 6a1de69c0..52ca0e870 100644 --- a/internal/sessions/codex_provider.go +++ b/internal/sessions/codex_provider.go @@ -195,12 +195,12 @@ func MoveCodexProvider(id, to string) (CodexMove, error) { if !codexProviderRe.MatchString(to) { return CodexMove{}, fmt.Errorf("%q isn't a provider id", to) } - dbReadMu.Lock() - defer dbReadMu.Unlock() + storeMu.Lock() + defer storeMu.Unlock() mu.Lock() defer mu.Unlock() loadCache() - defer closeDBs() + defer useDBs()() all := allFiles() groups := map[string][]file{} var mine []file diff --git a/internal/sessions/manage.go b/internal/sessions/manage.go index da52f5cd2..aa673df85 100644 --- a/internal/sessions/manage.go +++ b/internal/sessions/manage.go @@ -73,11 +73,11 @@ var ErrActive = errors.New("the session was written to in the last minute; it ma // Agents are the agents with sessions on this computer, by how many. func Agents() []AgentCount { - dbReadMu.Lock() - defer dbReadMu.Unlock() + storeMu.RLock() + defer storeMu.RUnlock() mu.Lock() defer mu.Unlock() - defer closeDBs() + defer useDBs()() return agentCounts(allFiles()) } @@ -105,12 +105,12 @@ func agentCounts(all []file) []AgentCount { // ListAgent is every session of one agent, the most recently active first, // with no limit; one nothing was said in is listed too, to be cleared away. func ListAgent(agent string) []Managed { - dbReadMu.Lock() - defer dbReadMu.Unlock() + storeMu.RLock() + defer storeMu.RUnlock() mu.Lock() defer mu.Unlock() loadCache() - defer closeDBs() + defer useDBs()() return listAgent(agent, allFiles()) } @@ -120,12 +120,12 @@ func ListAgent(agent string) []Managed { // agents' files are looked at, and of those only the ones whose session id // is inside the file are read when they changed. func NativeIDs(agents map[string]bool) map[string]map[string]bool { - dbReadMu.Lock() - defer dbReadMu.Unlock() + storeMu.RLock() + defer storeMu.RUnlock() mu.Lock() defer mu.Unlock() loadCache() - defer closeDBs() + defer useDBs()() return nativeIDs(agents, allFiles()) } @@ -146,12 +146,12 @@ type ManageView struct { // Only native's agents and the one picked are read: a change to another // agent's sessions waits for a page that shows it (#2776). func Manage(native map[string]bool, pick func([]AgentCount, map[string]map[string]bool) string) ManageView { - dbReadMu.Lock() - defer dbReadMu.Unlock() + storeMu.RLock() + defer storeMu.RUnlock() mu.Lock() defer mu.Unlock() loadCache() - defer closeDBs() + defer useDBs()() all := allFiles() v := ManageView{Agents: agentCounts(all), Native: nativeIDs(native, all)} v.Agent = pick(v.Agents, v.Native) @@ -298,12 +298,12 @@ func Delete(agent, id string) (Trashed, error) { if !safeID.MatchString(id) { return Trashed{}, errors.New("no such session") } - dbReadMu.Lock() - defer dbReadMu.Unlock() + storeMu.Lock() + defer storeMu.Unlock() mu.Lock() defer mu.Unlock() loadCache() - defer closeDBs() + defer useDBs()() all := allFiles() groups := map[string][]file{} var mine []file diff --git a/internal/sessions/opencode.go b/internal/sessions/opencode.go index 0e36617f0..60e1fff8c 100644 --- a/internal/sessions/opencode.go +++ b/internal/sessions/opencode.go @@ -8,6 +8,7 @@ import ( "path/filepath" "sort" "strings" + "sync" "time" "github.com/yetone/magpie/internal/appdir" @@ -170,9 +171,25 @@ func ocRoots(out []file, parent map[string]string) []file { // ---- the database ------------------------------------------------------------- // dbs are the databases open while List or Stats reads them, closed when -// they are done (closeDBs). +// the last of the reads using them is done (useDBs). var dbs = map[string]*sql.DB{} +// dbUsers is how many reads use dbs now. A read lets mu go while it parses +// with them, and another read may then start and end: it must not close +// what the first is still reading (#2776). +var dbUsers int + +// useDBs counts a read, with mu held, as a user of dbs until the release it +// returns is called, with mu held again; the last to go closes them. +func useDBs() (release func()) { + dbUsers++ + return sync.OnceFunc(func() { + if dbUsers--; dbUsers == 0 { + closeDBs() + } + }) +} + // Snapshot readers also remove their temporary database when a scan ends. var dbCleanup = map[string]func(){} diff --git a/internal/sessions/refresh_shared_test.go b/internal/sessions/refresh_shared_test.go new file mode 100644 index 000000000..57dd9d5ec --- /dev/null +++ b/internal/sessions/refresh_shared_test.go @@ -0,0 +1,171 @@ +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") + } +} diff --git a/internal/sessions/relocate_claude.go b/internal/sessions/relocate_claude.go index 9ae12f7c1..3d4b682e4 100644 --- a/internal/sessions/relocate_claude.go +++ b/internal/sessions/relocate_claude.go @@ -39,8 +39,8 @@ type ClaudeRelocationResult struct { // session-ID-keyed files (file-history, todos and session-env). IDs stay intact. // Clients must be closed: the in-process lock cannot exclude Claude itself. func RelocateClaudeProject(in ClaudeRelocation) (ClaudeRelocationResult, error) { - dbReadMu.Lock() - defer dbReadMu.Unlock() + storeMu.Lock() + defer storeMu.Unlock() mu.Lock() defer mu.Unlock() root, target, err := claudeRelocationRoot(in) diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go index a906d0033..1807a354e 100644 --- a/internal/sessions/sessions.go +++ b/internal/sessions/sessions.go @@ -615,10 +615,20 @@ func rolloutTwin(p string) string { } var ( - // DB readers share connection lifetimes; call-file readers need only mu. - dbReadMu sync.Mutex - mu sync.Mutex - cache map[string]*state // path → parse + // storeMu keeps the reads of the sessions apart from the changes made + // to their files (Delete, a provider move, a project relocation): a + // read takes it shared, so reads never wait on one another, and a + // change alone. The store handles they share live as long as their last + // user (useDBs), not as long as the read that opened them. + storeMu sync.RWMutex + mu sync.Mutex + cache map[string]*state // path → parse + // parsing holds the files a refresh is reading now, each closed when + // its parse is in the cache: another reader serves the parse kept of + // such a file rather than reading it a second time, and waits only for + // one it has no parse of yet (#2776: the start's warm-up held every + // page for its whole read) + parsing = map[string]chan struct{}{} loaded bool cacheGeneration uint64 // the zone the parses in memory date their days in @@ -770,15 +780,26 @@ func writeCache(c *save) { // cached agents alone, for a Codex-only identity refresh. func refresh(want, all []file) { defer func() { trimSummaryRevisions(time.Now()) }() - var todo []file + var todo, waits []file for _, f := range want { if f.cold { continue // in a stopped WSL distro: what was read of it stands } if s := cache[f.path]; s == nil || s.Size != f.size || s.Mod != f.mod.UnixNano() || (f.agent == "hermes" && (f.hermes == nil || f.rev == "" || s.DBRevision != f.rev)) || ((f.agent == "alma" || f.agent == "cursor") && s.DBRevision != f.rev) { + if parsing[f.path] != nil { + // another refresh is reading it: its last parse stands + // until that one is in, and one never read is waited for + if s == nil { + waits = append(waits, f) + } + continue + } todo = append(todo, f) } } + if len(waits) > 0 { + defer waitParsing(waits) + } gone := false if len(cache) > 0 && all != nil { on := make(map[string]bool, len(all)) @@ -821,6 +842,7 @@ func refresh(want, all []file) { jobs := make([]job, len(todo)) for i, f := range todo { jobs[i] = job{f: f, old: cache[f.path]} + parsing[f.path] = make(chan struct{}) } generation := cacheGeneration mu.Unlock() @@ -853,15 +875,16 @@ func refresh(want, all []file) { } close(ch) }() - // publish puts the parses finished since last time in the cache. - publish := func(done []int) { - for _, i := range done { - j := &jobs[i] - // A concurrent read may already have published a newer parse. - if generation == cacheGeneration && cache[j.f.path] == j.old { - cache[j.f.path] = j.parsed - } + // publish puts a finished parse in the cache at once, for a reader + // waiting on it, and lets the file be read again. + publish := func(i int) { + j := &jobs[i] + // A concurrent read may already have published a newer parse. + if generation == cacheGeneration && cache[j.f.path] == j.old { + cache[j.f.path] = j.parsed } + close(parsing[j.f.path]) + delete(parsing, j.f.path) } // What is read is kept on disk every checkpointEvery, not only at the // end: a first read of a long history on a slow disk runs for minutes, @@ -869,33 +892,65 @@ func refresh(want, all []file) { // of it, so the next start read it all again. tick := time.NewTicker(checkpointEvery) defer tick.Stop() - var done []int + unsaved := false for n := 0; n < len(jobs); { select { case i := <-finished: - done = append(done, i) + mu.Lock() + publish(i) + mu.Unlock() + unsaved = true n++ case <-tick.C: - if len(done) == 0 { + if !unsaved { continue } mu.Lock() - publish(done) if generation == cacheGeneration { saveCache() } mu.Unlock() - done = done[:0] + unsaved = false } } mu.Lock() - publish(done) progress.finish() if generation == cacheGeneration { saveCache() } } +// waitParsing waits, with mu let go, for the files other refreshes are +// reading, and reads itself any of them that came to nothing (a Reset +// dropped its parse). +func waitParsing(fs []file) { + for { + var chans []chan struct{} + for _, f := range fs { + if ch := parsing[f.path]; ch != nil { + chans = append(chans, ch) + } + } + if len(chans) == 0 { + break + } + mu.Unlock() + for _, ch := range chans { + <-ch + } + mu.Lock() + } + var missing []file + for _, f := range fs { + if cache[f.path] == nil { + missing = append(missing, f) + } + } + if len(missing) > 0 { + refresh(missing, nil) + } +} + // checkpointEvery is how often a long read of changed files keeps what it // has read so far on disk. var checkpointEvery = 5 * time.Second @@ -1008,8 +1063,8 @@ func Reset() { // List reads the latest sessions of every agent, the most recently active // first, at most limit of them (Limit when 0). func List(limit int) []Session { - dbReadMu.Lock() - defer dbReadMu.Unlock() + storeMu.RLock() + defer storeMu.RUnlock() if limit <= 0 { limit = Limit } @@ -1017,7 +1072,7 @@ func List(limit int) []Session { defer mu.Unlock() loadCache() - defer closeDBs() + defer useDBs()() files := allFiles() groups := map[string][]file{} latest := map[string]time.Time{} @@ -1627,7 +1682,7 @@ func Titles(keys []string) map[string]string { mu.Lock() defer mu.Unlock() loadCache() - defer closeDBs() + defer useDBs()() files := allFiles() groups := map[string][]file{} var fs []file @@ -1675,7 +1730,7 @@ func get(key string) (s Session, ok, found bool) { mu.Lock() defer mu.Unlock() loadCache() - defer closeDBs() + defer useDBs()() files := allFiles() var fs []file for _, f := range files { diff --git a/internal/sessions/stats.go b/internal/sessions/stats.go index c8035dc3e..67c8a10de 100644 --- a/internal/sessions/stats.go +++ b/internal/sessions/stats.go @@ -131,12 +131,12 @@ func statsIn(days int, now time.Time, native map[string]bool) Stats { } out := Stats{From: from, To: today.Format(time.DateOnly), Days: []Day{}, Sessions: []Summary{}, Keys: []string{}} - dbReadMu.Lock() - defer dbReadMu.Unlock() + storeMu.RLock() + defer storeMu.RUnlock() mu.Lock() defer mu.Unlock() loadCache() - defer closeDBs() + defer useDBs()() files := allFiles() // every changed file, whatever the range, so a wider range picked later // finds them read rather than starting another run diff --git a/internal/sessions/traces_opencode_db.go b/internal/sessions/traces_opencode_db.go index cfd386f3f..74140ee54 100644 --- a/internal/sessions/traces_opencode_db.go +++ b/internal/sessions/traces_opencode_db.go @@ -12,7 +12,7 @@ import ( // These connections belong to the trace reader, not the Sessions listing cache. // Updates are read in timestamp/ID order in bounded batches; large histories -// neither hold dbReadMu nor force the entire conversation to be re-exported. +// neither hold storeMu nor force the entire conversation to be re-exported. type ocTraceState struct { messages map[string]ocTraceMessage order []string diff --git a/internal/sessions/traces_review_test.go b/internal/sessions/traces_review_test.go index c5ab8f71f..9af8a793b 100644 --- a/internal/sessions/traces_review_test.go +++ b/internal/sessions/traces_review_test.go @@ -156,15 +156,15 @@ func TestOpenCodeTraceLargeSessionProgressAndIncrementalUpdate(t *testing.T) { for i := 0; i < 12; i++ { budget := int64(8 << 20) // Trace reading must not acquire the Sessions listing mutex. - dbReadMu.Lock() + storeMu.Lock() done := make(chan []TraceSpan, 1) go func() { done <- r.pollOpenCode(false, &budget) }() var spans []TraceSpan select { case spans = <-done: - dbReadMu.Unlock() + storeMu.Unlock() case <-time.After(10 * time.Second): - dbReadMu.Unlock() + storeMu.Unlock() <-done t.Fatal("trace reading blocked on the listing mutex") } diff --git a/internal/sessions/transcript_opencode.go b/internal/sessions/transcript_opencode.go index 833616feb..92b6d7fc4 100644 --- a/internal/sessions/transcript_opencode.go +++ b/internal/sessions/transcript_opencode.go @@ -8,10 +8,20 @@ import ( // openCodeTranscript reads the conversation in message order from the same // store the listing chose, including while a v1 to v2 migration is underway. func openCodeTranscript(s Session, add func(bool, Part) bool) error { - dbReadMu.Lock() - defer dbReadMu.Unlock() - defer closeDBs() - for _, f := range openCodeFiles() { + storeMu.RLock() + defer storeMu.RUnlock() + // the store handles are shared with the reads that hold mu: they are + // opened and given back under it, and read from without it + mu.Lock() + release := useDBs() + files := openCodeFiles() + mu.Unlock() + defer func() { + mu.Lock() + release() + mu.Unlock() + }() + for _, f := range files { if f.sid != s.ID { continue }