Files
magpie/internal/library/claude_json_lock_test.go
T
ggbdpq c57ac416d3 fix(library): MCP writes to Claude Code's .claude.json take its save lock (#4747)
What changed: the Library gave Claude Code its MCP servers by reading
~/.claude.json (or $CLAUDE_CONFIG_DIR's) and writing it back with no
lock held, while Claude Code saves the same file often (project state,
tips, usage counters) under a lock of its own, <the file's real
path>.lock: a save of its own between the Library's read and write was
lost. The lock helpers move out of internal/provider into a small
claudelock package (the mkdir lock Claude Code 2.1.316 takes, stale
after 10s for this file), putClaudeLogin keeps its #4532 behavior
through wrappers, and the Library's mcpFile.putEntry and mcpFile.del
take <file>.lock around the .claude.json read-modify-write: they wait
while Claude Code holds it, take over one left stale, and fail the
server's write with nothing changed when it holds one past 20s. A WSL
distro's copy is left as it was: the Claude Code there saves under a
lock in the distro's own files.

Run (mote, -tags nogui):
- TestClaudeJSONMCPWaitsForClaudeCodesLock and
  TestClaudeJSONMCPTakesAStaleLock (internal/library): red on the old
  code (saved while Claude Code held the lock; a stale lock left
  behind), green now.
- go test ./internal/library ./internal/provider -tags nogui: both
  packages green, the #4532 lock tests among them (TestClaudeSwitch…
  refresh/legacy/config, stale takeover, held past the wait).
- race on the lock tests of both packages; go vet on the three touched
  packages for darwin, linux and windows; gofmt clean; go build -tags
  nogui for darwin, linux and windows.

Generated-by: GLM-5.3-Flash (ZCode)
2026-11-02 05:38:11 +08:00

93 lines
2.8 KiB
Go

package library
import (
"os"
"path/filepath"
"strings"
"testing"
"time"
)
// #4747: a server given to Claude Code is written into its .claude.json
// under the lock Claude Code saves the file under (<the file's real
// path>.lock, a directory): held while the file is read and written back,
// or a save of its own between the two — project state, tips, usage
// counters — is lost.
// A server is not written while Claude Code holds the lock, and goes in
// once it lets it go, the lock taken and let go as Claude Code takes and
// lets its own go.
func TestClaudeJSONMCPWaitsForClaudeCodesLock(t *testing.T) {
h := sandbox(t)
profile := filepath.Join(h, ".claude.json")
write(t, profile, "{}\n")
lock := profile + ".lock"
if err := os.Mkdir(lock, 0o755); err != nil { // Claude Code holds it
t.Fatal(err)
}
done := make(chan *Result, 1)
errs := make(chan error, 1)
go func() {
r, err := SaveServer("", Server{Name: "fs", Transport: "stdio", Command: "npx", Args: []string{"-y", "@mcp/fs"}, Agents: []string{"claude"}})
done <- r
errs <- err
}()
// it waits as long as the lock is held, and touched
for range 5 {
select {
case <-done:
t.Fatal("saved while Claude Code held its lock")
case <-time.After(100 * time.Millisecond):
}
now := time.Now()
_ = os.Chtimes(lock, now, now)
}
if err := os.Remove(lock); err != nil { // Claude Code lets it go
t.Fatal(err)
}
select {
case r := <-done:
if err := <-errs; err != nil {
t.Fatal(err)
}
if len(r.Problems) > 0 {
t.Fatalf("problems: %+v", r.Problems)
}
case <-time.After(30 * time.Second):
t.Fatal("the server never went in after the lock was let go")
}
if got := read(t, profile); !strings.Contains(got, "@mcp/fs") {
t.Fatalf("the server isn't in .claude.json:\n%s", got)
}
if _, err := os.Stat(lock); !os.IsNotExist(err) {
t.Fatalf("the lock left behind: %v", err)
}
}
// A lock its holder left (not touched for longer than Claude Code's stale
// time for the file, 10s) is taken over, as Claude Code does.
func TestClaudeJSONMCPTakesAStaleLock(t *testing.T) {
h := sandbox(t)
profile := filepath.Join(h, ".claude.json")
write(t, profile, "{}\n")
lock := profile + ".lock"
if err := os.Mkdir(lock, 0o755); err != nil {
t.Fatal(err)
}
old := time.Now().Add(-2 * time.Minute)
_ = os.Chtimes(lock, old, old)
r, err := SaveServer("", Server{Name: "fs", Transport: "stdio", Command: "npx", Args: []string{"-y", "@mcp/fs"}, Agents: []string{"claude"}})
if err != nil {
t.Fatal(err)
}
if len(r.Problems) > 0 {
t.Fatalf("problems: %+v", r.Problems)
}
if got := read(t, profile); !strings.Contains(got, "@mcp/fs") {
t.Fatalf("the server isn't in .claude.json:\n%s", got)
}
if _, err := os.Stat(lock); !os.IsNotExist(err) {
t.Fatalf("the lock left behind: %v", err)
}
}