Files
magpie/internal/library/inuse_unlocked_test.go
T
yetone 4ef718651f library: InUse answers while a sync holds the library's lock
#2774: the macOS window stayed white at start. The window waits on
boot.js before it paints, and boot.js asks library.InUse through the
built-in plugins. InUse took the library's mu, which a sync holds for
its whole run: 13-16s on the reporter's Mac, with many agents and
skills. InUse only reads library.json, and save replaces that file whole
(edit.WriteAtomic), so InUse now reads it without mu. A read beside a
sync sees the file from before or after one of the sync's writes, never
half of one. A file that can't be read is still an error, never 0.

TestInUseAnswersWhileASyncHoldsTheLock holds mu and expects InUse to
return 3 within 5s. It also expects a half-written file to be an error.
With the lock put back by hand, it goes red:
  --- FAIL: TestInUseAnswersWhileASyncHoldsTheLock (14.32s)
      inuse_unlocked_test.go:36: InUse waited on the sync's lock

The sandbox cold start measured for this work has a short library sync,
so boot.js came at about the same time before and after (4527ms against
4822ms after listen at ~4s). The reporter's 13-16s sync wasn't
reproduced here.

What was run (worktree at 9fd80b212 plus this change and the commits
before it):
  gofmt -l internal/: nothing
  go vet ./internal/library: exit 0
  GOOS=linux go vet -tags nogui ./...: exit 0
  GOOS=windows go vet -tags nogui ./...: exit 0
  go test -timeout 45m -count=1 ./internal/library: ok 1220.249s
2026-10-20 20:23:56 +08:00

45 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package library
import (
"os"
"path/filepath"
"testing"
"time"
)
// #2774: boot.js, which the window waits on before it paints, asks InUse
// through the built-in plugins, and a library sync holds mu for as long as
// it takes (13–16s on the reporter's Mac): the window stayed white that
// long. InUse answers while a sync holds the lock, from the file as it is.
func TestInUseAnswersWhileASyncHoldsTheLock(t *testing.T) {
sandbox(t)
os.MkdirAll(filepath.Dir(path()), 0o700)
write(t, path(), `{"skills":[{"name":"a"},{"name":"b"}],"mcp":[{"name":"s"}]}`)
mu.Lock() // a sync, still running
defer mu.Unlock()
type res struct {
n int
err error
}
got := make(chan res, 1)
go func() {
n, err := InUse()
got <- res{n, err}
}()
select {
case r := <-got:
if r.err != nil || r.n != 3 {
t.Fatalf("InUse = %d, %v; want 3", r.n, r.err)
}
case <-time.After(5 * time.Second):
t.Fatal("InUse waited on the sync's lock")
}
// a file that can't be read is still an error, not 0
write(t, path(), `{"skills":`)
if n, err := InUse(); err == nil {
t.Fatalf("a broken library.json read as %d", n)
}
}