heldFiles reused a listing taken after the latest hold began, judged by
time.Now(). On Windows two time.Now() a moment apart can be equal, so a
hold begun in the tick a listing was taken in read that older listing
as its own: TestOneListing red. Its t.Fatalf came before done(), so
the package-global hold stayed set and every later test read a stale
listing: TestPurgeOne, TestPurgeAll and TestUnchangedPackedNotReadAgain
red, and TestShareListing / TestReadNotHeldByAnotherRead waited forever
(the 45m package timeout in #4114 and #4177).
- held.gen counts holds begun; a listing carries the gen it was taken
under and is reused only under the same gen. No clock is read.
- holdsEnd(t) ends any hold a test left, so one red names itself and
doesn't take the package down with it.
- TestShareListing, TestReadNotHeldByAnotherRead and
TestReadWaitsForAFirstReadUnderWay select on the other channel too,
so a missed listing fails at once instead of hanging.
Run (Windows: win-run on xiaomi-gpu; macOS: mote-run; Linux: wsl-run):
- Windows, old one_listing.go: TestOneListing red alone. Fixed:
TestOneListing and TestShareListing -count=500 green.
- Windows, fix without holdsEnd: the cascade comes back with each test
red and named, no hang. With it: internal/sessions green but for
TestAgySessions/TestAgyRowOnly (#4115, red at main too).
- macOS and Linux: internal/sessions ok, at the rebased head too.
- GOOS=windows go vet ./..., GOOS=linux go vet -tags nogui ./..., go vet
./... on mote: ok. gofmt -l clean.
Broken by hand: the time comparison put back (red on Windows, above);
holdsEnd removed (cascade, above).
Not tried: the full go test ./... on mote (six other suites were
running there; owner rule: don't wait behind it).
At the rebased head (on origin/main 033b17baf): Windows internal/sessions
red only in TestAgySessions/TestAgyRowOnly (#4115), internal/testenv ok,
gui TestGatewaySessionHistoryAndCalendar + TestUsageLedger*Routes ok;
macOS and Linux internal/sessions and internal/testenv ok.
Since c381e4cd1 (#2776) each file a refresh reads has an
entry in parsing, closed and deleted when its parse is published.
refresh checked parsing[f.path] while picking the files to read but set
the entry only later, while building the jobs, so a path listed twice
in one want passed the check twice: two jobs on one entry, and the
second publish closed a nil channel. The start's warm-up
(warmSessions -> StatsAndNative -> refresh) took magpie down a few
seconds after every start, in the GUI, `magpie web` and --gateway alike.
Reported by TimWongUp, nativeas, tianhao2316-beep, mxl2498, cunnak and
Tsuki-hash, on Windows and macOS.
Two sources of the duplicate were found in the thread:
- CODEBUDDY_CONFIG_DIR on WorkBuddy's folder (TimWongUp's .zshrc;
on Windows WorkBuddy's installer, or its IDE host for its children,
sets both variables to one .workbuddy): codebuddyFiles and
workbuddyFiles listed each .jsonl once each.
- A Hermes state.db holding one session id as a TEXT and a BLOB row
(nativeas): both scan to one string, one <db>#<id> path.
The change, JoeyWu-tech's fix/2932-rebased (7cc938ca) with two test
changes:
- refresh marks a file in parsing as it takes it, so a second listing
of the path is left out (or waited on). This is the guard: whatever
list repeats a path, no refresh reads it twice.
- allFiles and callSources keep each path once (distinctPaths), so a
path is not counted twice either. OpenCode's list doubles the same
TEXT/BLOB rows (tested); ZCode's and Alma's would too.
- hermesFiles keeps one file a session id, as late as its later row.
- codebuddyFiles lists nothing when CodeBuddy Code's folder is
WorkBuddy's (os.SameFile: a link or Windows' case rules count too),
so those sessions are WorkBuddy's on the Sessions page and in usage;
Dirs names such a folder once. A session the CodeBuddy CLI wrote
there shows as WorkBuddy's (agreed by TimWongUp on the issue).
Added here: TestCodeBuddyInWorkBuddysFolderIsReadOnce has a "both"
case, both variables on one folder as the Windows reports have it, and
runs StatsAndNative(0) first, the warm-up's own call.
TestOpenCodeIDStoredAsTextAndBlobIsListedOnce lists under mu with
useDBs, as List does: it left OpenCode's db open, and on Windows its
TempDir could not be removed ("being used by another process").
Red on origin/main (sessions code of 86254d404, these tests, a test-only
sameFolder shim): TestRefreshReadsAPathListedTwiceOnce,
TestCodeBuddyInWorkBuddysFolderIsReadOnce (named and both) and
TestHermesIDStoredAsTextAndBlobIsOneSession panic "close of nil
channel" at sessions.go:891 from refresh at :905; the "both" case from
stats.go:143 and :117, the reports' frames.
TestOpenCodeIDStoredAsTextAndBlobIsListedOnce: "allFiles lists the
session 2 times", the same for callSources. Same on the Windows box.
Broken by hand: with only the refresh marking undone, the refresh test
panics again; with codebuddyFiles' check off, the CodeBuddy test fails
"calls ... codebuddy:6, want WorkBuddy's 6" and "listed 3 WorkBuddy and
3 CodeBuddy Code sessions, want 3 and 0". Restored, green.
Run:
- Full suite, `go test -timeout 45m ./...` (pipefail, log kept whole),
darwin arm64, go1.26.8, at 31136c446 (86254d404 + these two
changes, before the OpenCode test's useDBs change): 68 packages ok,
no FAIL; internal/sessions 158s, internal/gateway 579s. No suite lock
exists on this machine; no other full suite was running.
- At the pushed head (rebased on ec7e50b82, a redact-only change):
go test ./internal/sessions/ ok, -race ./internal/sessions/ ok
(109s); -race -count=5 on the four new tests and
TestReadNotHeldByAnotherRead ok earlier.
- Windows 11 box (cross-compiled test binary, testenv's sandbox HOME):
the whole internal/sessions package PASS with the fix, the linked
case included; the new tests red on main's code as above.
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows:
ok.
Not tried: a real WorkBuddy/CodeBuddy install, a real Hermes state.db
with a BLOB id, or the app's GUI start on Windows; the tests use the
fixtures under the reporters' settings. Not covered: two agents told
to share a folder under different spellings other than CodeBuddy and
WorkBuddy (Pi and omp, Qoder and Qoder CN) still list it twice, without
the crash: #3258.
Co-authored-by: JoeyWu-tech <71250493+JoeyWu-tech@users.noreply.github.com>
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), base 1a3b92688 against
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/main 2ceff7370 with 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.