sessions: a read is never held by another read's refresh, only by a change to the files (#2776)
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.
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
+15
-15
@@ -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
|
||||
|
||||
@@ -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(){}
|
||||
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user