Files
magpie/docs
yetone 0b7d5f3a20 claude: a switch waits for Claude Code's own refresh locks (#4532)
A switch read the sign-in it was replacing while Claude Code could be
refreshing it. Claude Code 2.1.316 refreshes under two mkdir locks,
<config dir>/.oauth_refresh.lock then <realpath(config dir)>.lock
(~/.claude.lock), both stale after 60s, and saves the rotated pair with a
compare-and-swap on the refresh token. So the outgoing account's token
was not written over the incoming one, but magpie kept the outgoing
account with the refresh token that refresh spent, and it was signed out
of magpie on its next use.

switchSavedLogin and addLogin (Claude) now take both locks the way
Claude Code takes them (claude_lock.go: mkdir, touch while held, take
over one untouched past the stale time, let go in reverse order) before
reading the live sign-in, and hold them until the new one is written.
putClaudeLogin's ~/.claude.json rewrite takes <real path>.lock (stale
10s), after the refresh locks, the order Claude Code saves in. A lock
held past 20s fails the switch with nothing written.

Read from Claude Code 2.1.316's bundled source: lgr/Cki/cl (refresh
locks, owner record, retries), Vno (CAS write), gi (config save lock).

Run (mote, -tags nogui):
- TestClaudeSwitchWaitsForClaudeCodesRefresh (refresh/legacy/config) and
  TestClaudeSwitchStaleAndHeldLocks: red on the old code (switched while
  each lock was held; no error when held), green now. Also green on
  Windows (win-run).
- Broken by hand: no stale takeover -> stale subtest red (20s, locked).
- go test ./internal/provider -run 'TestClaudeSwitch|TestClaudeAccount|Login'
- go vet ./internal/provider for darwin, linux, windows; gofmt clean.

Not tried: a real Claude Code refreshing while a real switch runs (its
refresh fires only near expiry). magpie writes no
.oauth_refresh.lock.owner, so Claude Code takes over a lock magpie left
only by its 60s stale time. The Library's MCP writes to ~/.claude.json
don't take the config lock yet: #4747.
2026-11-01 15:02:38 +08:00
..