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)
66 lines
2.3 KiB
Go
66 lines
2.3 KiB
Go
package provider
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/yetone/magpie/internal/appdir"
|
|
"github.com/yetone/magpie/internal/claudelock"
|
|
)
|
|
|
|
// Claude Code refreshes its sign-in under two of its own locks, in this
|
|
// order: <config dir>/.oauth_refresh.lock, then the older <config
|
|
// dir>.lock (~/.claude.lock), both stale after 60s. Under them it reads
|
|
// the sign-in again, posts the refresh token, and writes back the new
|
|
// pair. A switch that read the sign-in it was replacing while a refresh
|
|
// ran kept that account's refresh token as it was before the refresh:
|
|
// spent, and the account signed out of magpie on its next use (#4532).
|
|
// It saves ~/.claude.json under <that file>.lock, stale after 10s, while
|
|
// it may hold the refresh locks: so it is taken after them, as Claude
|
|
// Code does. The locks are mkdir locks, taken as Claude Code takes them
|
|
// (claudelock).
|
|
|
|
// errClaudeLocked is a lock Claude Code held past claudelock.Wait.
|
|
var errClaudeLocked = errors.New("Claude Code is refreshing its sign-in: try again in a moment")
|
|
|
|
// claudeRefreshLocks takes the locks Claude Code refreshes its sign-in
|
|
// under, for the config directory magpie reads the sign-in of. The
|
|
// release lets them go in the reverse order. A directory not there yet
|
|
// (Claude Code never run) has no refresh to wait for.
|
|
func claudeRefreshLocks() (release func(), err error) {
|
|
dir := appdir.ClaudeDir()
|
|
if fi, err := os.Stat(dir); err != nil || !fi.IsDir() {
|
|
return func() {}, nil
|
|
}
|
|
first, err := claudelock.Take(filepath.Join(dir, ".oauth_refresh.lock"), claudelock.RefreshStale)
|
|
if err != nil {
|
|
return nil, claudeHeld(err)
|
|
}
|
|
resolved := dir
|
|
if r, err := filepath.EvalSymlinks(dir); err == nil {
|
|
resolved = r
|
|
}
|
|
legacy, err := claudelock.Take(resolved+".lock", claudelock.RefreshStale)
|
|
if err != nil {
|
|
first()
|
|
return nil, claudeHeld(err)
|
|
}
|
|
return func() { legacy(); first() }, nil
|
|
}
|
|
|
|
// claudeConfigLock takes the lock Claude Code saves ~/.claude.json under.
|
|
func claudeConfigLock() (release func(), err error) {
|
|
release, err = claudelock.Config(claudeProfilePath())
|
|
return release, claudeHeld(err)
|
|
}
|
|
|
|
// claudeHeld says a lock Claude Code held past the wait as a switch's
|
|
// own: the one waiting on a refresh, not on a save.
|
|
func claudeHeld(err error) error {
|
|
if errors.Is(err, claudelock.ErrHeld) {
|
|
return errClaudeLocked
|
|
}
|
|
return err
|
|
}
|