claudeCredential reads under claudeCacheMu, and the hex item (#70) was written again through saveClaudeCredential, which caches under the same lock. sync.Mutex isn't reentrant, so the read held the lock for good and every later read of the Claude sign-in (the account, its usage, the gateway's account reads) waited on it. The rewrite now only writes (writeClaudeCredential); the read caches what it read as before. Taken from #1126 at 834f2b9e and resolved onto main: since31956b92the hex rewrite is `if i.hex { ... }` on readClaudeKeychainItem's result, and saveClaudeCredential begins with the discoveryOff check, which moves into writeClaudeCredential so both writes keep it. Run on5b14ebf49+ this, sandbox HOME, pinned caches, -tags nogui: - TestClaudeKeychainHexIsWrittenAgainOnRead with main's account.go: "panic: reading Claude Code's sign-in hung", stack through cacheClaudeCredential <- saveClaudeCredential <- readClaudeCredential <- claudeCredential. With the change: passes. - -race -count=10 of it, TestKeychainText, TestClaudeKeychain*, TestClaudeMarshal*: ok. go test ./internal/provider (-timeout 45m; it took 843s on a loaded machine and passes the default 10m only when idle): ok. - gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows on internal/provider: clean. - The test's `security` is a fake on PATH (testenv also shadows it), so no real keychain is read or written. Not tried against a real keychain.
88 lines
2.6 KiB
Go
88 lines
2.6 KiB
Go
package provider
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime/debug"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// A sign-in magpie wrote on several lines, read through the cache from the
|
|
// keychain as hex, is written again on one line without the read hanging:
|
|
// the write used to cache it under the lock the read held, and every later
|
|
// read of Claude Code's sign-in waited on it for good.
|
|
func TestClaudeKeychainHexIsWrittenAgainOnRead(t *testing.T) {
|
|
shellFakes(t)
|
|
home := claudeHome(t)
|
|
t.Setenv("CLAUDE_CONFIG_DIR", "")
|
|
t.Setenv("USER", "tester")
|
|
c := claudeCredentials{OAuth: claudeAuth{AccessToken: "live", SubscriptionType: "max"}}
|
|
indented, _ := c.marshal()
|
|
bin := filepath.Join(home, "bin")
|
|
os.MkdirAll(bin, 0o755)
|
|
item := filepath.Join(bin, "item")
|
|
if err := os.WriteFile(item, []byte(hex.EncodeToString(indented)+"\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
testenv.Program(t, filepath.Join(bin, "security"), `#!/bin/sh
|
|
case "$1" in
|
|
find-generic-password) cat '`+item+`' ;;
|
|
add-generic-password)
|
|
while [ $# -gt 0 ]; do
|
|
if [ "$1" = -w ]; then printf '%s\n' "$2" > '`+item+`'; fi
|
|
shift
|
|
done ;;
|
|
*) exit 1 ;;
|
|
esac
|
|
`)
|
|
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
|
|
claudeKeychain = true
|
|
forgetClaudeCredential()
|
|
|
|
read := func() (claudeCredentials, claudeCredentialLocation, bool) {
|
|
type result struct {
|
|
c claudeCredentials
|
|
loc claudeCredentialLocation
|
|
ok bool
|
|
}
|
|
done := make(chan result, 1)
|
|
go func() {
|
|
c, loc, ok := claudeCredential()
|
|
done <- result{c, loc, ok}
|
|
}()
|
|
select {
|
|
case r := <-done:
|
|
return r.c, r.loc, r.ok
|
|
case <-time.After(10 * time.Second):
|
|
// the hung read holds claudeCacheMu, which isolate's cleanup
|
|
// takes: t.Fatal, or a panic here, would sit out -timeout
|
|
// running it. A panic off the test's goroutine ends the binary
|
|
// now, with every goroutine's stack
|
|
debug.SetTraceback("all")
|
|
go func() { panic("reading Claude Code's sign-in hung") }()
|
|
select {}
|
|
}
|
|
}
|
|
got, loc, ok := read()
|
|
if !ok || got.OAuth.AccessToken != "live" || !loc.keychain || loc.account != "tester" {
|
|
t.Fatalf("read %v %+v %+v", ok, got.OAuth, loc)
|
|
}
|
|
b, _ := os.ReadFile(item)
|
|
if line := strings.TrimSpace(string(b)); strings.Contains(line, "\n") || !strings.HasPrefix(line, "{") {
|
|
t.Fatalf("not written again on one line: %q", b)
|
|
}
|
|
// the cache is served, and read again once forgotten
|
|
if got, _, ok := read(); !ok || got.OAuth.AccessToken != "live" {
|
|
t.Fatalf("cached read %v %+v", ok, got.OAuth)
|
|
}
|
|
forgetClaudeCredential()
|
|
if got, _, ok := read(); !ok || got.OAuth.AccessToken != "live" {
|
|
t.Fatalf("read again %v %+v", ok, got.OAuth)
|
|
}
|
|
}
|