Files
magpie/internal/plugin/plugin_renew_race_test.go
T
yetone bac5be0677 plugin: one renewal of a sign-in at a time across magpie processes (plugins #331)
Cause: every magpie process that loads plugins runs a plugin host of its
own (the gateway, a CLI quota or usage command, a host restarting while
the old one finishes), and host.js's renew() kept a sign-in's renewal to
one at a time only inside one host (the in-memory renewing map). Two
hosts that found the same sign-in due each called the plugin's
auth.refresh with the same refresh token. A vendor that spends a refresh
token once (MiniMax, and about ten plugins' vendors: cline, codearts,
qoder, trae, workbuddy, raccoon, nous, kiro, factory) turned the second
away with invalid_grant, the plugin threw signIn "expired", renew() sent
it on without looking at the store again, and host.go -> provider's
notePluginLapse marked the account signed out, though the first host
had already saved a good token.

What changed, in internal/plugin/host.js only, for every plugin:
- renew() takes a lock file of the account's beside the store,
  plugin-auth.json.renew-<sha256(key)[:16]>.lock, holding the holder's
  pid and start time, across the plugin's auth.refresh, and lets it go
  in finally. It is separate from plugin-auth.json.lock, which changeAuth
  holds only for a write and never across an await. A lock whose pid
  is gone (process.kill(pid, 0), EPERM counted alive) is taken over at
  once, one older than 60s by anyone; a live holder is waited for with
  an async backoff (10ms to 500ms), never blocking the host's loop, and
  never past 120s. A lock is removed only while it holds the bytes read.
- Under the lock the sign-in is read again from the file (past
  readAuth's stat cache). Changed since the renewal began (another host
  renewed it, or it was signed out or in again), the renewal ends
  without calling the plugin; no longer due, likewise.
- A refresh that throws while the stored sign-in changed meanwhile (a
  magpie from before this lock spent the token and saved the new one)
  is logged, not reported expired.

Tests (internal/plugin/plugin_renew_race_test.go, fixture
testdata/renewrace): a fake vendor on loopback whose refresh tokens are
single-use.
- TestRenewOnceAcrossHosts: two hosts in two processes (this test binary
  and a child running TestRenewOtherHost) on one sandbox store. The
  first host's exchange is held until the second host is renewing too,
  waited on as state. One exchange, neither host says expired, both
  requests go with the new token, the store has it.
- TestRenewLockOfDeadHostAndRefusal: a lock naming a pid that has ended
  is taken over inside the request's wait and removed; a token the
  vendor never gave still says expired and leaves the account as it
  was; one refused after another magpie saved a new token says nothing
  and the next request goes with that token.

Broken by hand, each red:
- no lock (lockRenew returns at once): TestRenewOnceAcrossHosts red with
  exchanges [r1 r1], the other host said [expired], a request sent
  a-old - the issue's symptom; the dead-pid test red (lock left).
- no re-read under the lock (now = was): exchanges [r1 r1].
- the catch's check removed: "a token another magpie renewed meanwhile
  said expired".
- a dead pid counted alive: the dead-pid test red after its wait.
(Removing only the re-read's "changed" branch stays green: the "no
longer due" check under the lock catches the same case.)

Run at c909ca5e4 (on origin/main 84d902ce2): the new tests and
TestPluginRenewsSignIns -count=3, and -race -count=10 for the new ones;
gofmt -l; go vet ./... on darwin, GOOS=linux -tags nogui, GOOS=windows;
set -o pipefail; go test -tags nogui -timeout 45m ./... tee'd to a
log: all 88 packages pass (69 ok, 19 no test files), none red
(gateway 574s, agent 431s, plugin 265s, provider 246s). origin/main
was still 84d902ce2 at push.

Not tried: a real vendor account racing (MiniMax or another); Windows
and Linux runs of the new tests (process.kill(pid, 0) in Bun on Windows
is expected to work as in Node, not seen); the GUI suite (internal/gui
untouched). Not changed: a plugin that refreshes inside its own loader
(MiniMax's fetch on a 401, or within EARLY_MS) still de-dupes only in
its process; magpie's renewal comes before that window, so it is the
rarer path.

Reported by 1007cordon-ux (plugins #331).
2026-10-23 14:26:43 +08:00

339 lines
10 KiB
Go

package plugin
import (
"bufio"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"testing"
"time"
)
// raceVendor is a vendor that spends a refresh token once (plugins
// #331): the first exchange of r1 gives r2, and r1 again, or a token it
// never gave, is invalid_grant. hold, when set, keeps the first exchange
// from answering until it is closed, and arrived says it came.
type raceVendor struct {
mu sync.Mutex
valid map[string]string // refresh token → the one it gives
exchanges []string
hold chan struct{}
arrived chan struct{}
auths []string // each request's Authorization
store string // plugin-auth.json
}
func newRaceVendor(t *testing.T) *raceVendor {
t.Helper()
v := &raceVendor{valid: map[string]string{"r1": "r2"}, arrived: make(chan struct{}, 8), store: AuthPath()}
tokens := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
v.mu.Lock()
v.exchanges = append(v.exchanges, string(b))
next, ok := v.valid[string(b)]
delete(v.valid, string(b)) // spent, whatever comes of the answer
first := len(v.exchanges) == 1
v.mu.Unlock()
v.arrived <- struct{}{}
if first && v.hold != nil {
<-v.hold
}
if key, ok := strings.CutPrefix(string(b), "r-raced "); ok {
// a magpie from before the lock spent it first and saved
// what it got, then this one is turned away
var all map[string]map[string]any
raw, _ := os.ReadFile(v.store)
json.Unmarshal(raw, &all)
all[key]["access"], all[key]["refresh"] = "a-elsewhere", "r-elsewhere"
all[key]["expires"] = time.Now().Add(time.Hour).UnixMilli()
raw, _ = json.Marshal(all)
os.WriteFile(v.store, raw, 0o600)
}
if !ok {
w.WriteHeader(400)
io.WriteString(w, `{"error":"invalid_grant"}`)
return
}
fmt.Fprintf(w, `{"access":"a-%s","refresh":%q}`, next, next)
}))
t.Cleanup(tokens.Close)
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
v.mu.Lock()
v.auths = append(v.auths, r.Header.Get("Authorization"))
v.mu.Unlock()
io.WriteString(w, "ok")
}))
t.Cleanup(api.Close)
t.Setenv("RACE_TOKEN_URL", tokens.URL)
t.Setenv("FAKE_BASE", api.URL+"/v1")
return v
}
func (v *raceVendor) seen() (exchanges, auths []string) {
v.mu.Lock()
defer v.mu.Unlock()
return append([]string(nil), v.exchanges...), append([]string(nil), v.auths...)
}
// raceAccount adds the race plugin and an account due for renewal.
func raceAccount(t *testing.T, ctx context.Context, refresh string) string {
t.Helper()
abs, _ := filepath.Abs("testdata/renewrace/index.js")
if _, err := Add(ctx, abs); err != nil {
t.Fatal(err)
}
soon := time.Now().Add(time.Minute).UnixMilli() // inside the 5 minutes
k, err := Import(ctx, "raceco", map[string]any{"type": "oauth", "refresh": refresh, "access": "a-old", "expires": soon, "accountId": "me@race"})
if err != nil {
t.Fatal(err)
}
return k
}
func raceSend(ctx context.Context, account string) error {
res, err := Fetch(ctx, FetchRequest{Provider: "raceco", Account: account, Model: "race-1", NPM: "@ai-sdk/openai-compatible",
URL: os.Getenv("FAKE_BASE") + "/chat/completions", Method: "POST", Body: []byte(`{}`)})
if err != nil {
return err
}
io.ReadAll(res.Body)
return res.Body.Close()
}
func raceStored(key string) map[string]any {
var all map[string]map[string]any
b, _ := os.ReadFile(AuthPath())
json.Unmarshal(b, &all)
return all[key]
}
// changeStored sets the refresh token of the account at key in the store.
func changeStored(key, refresh string) error {
var all map[string]map[string]any
raw, err := os.ReadFile(AuthPath())
if err != nil {
return err
}
if err := json.Unmarshal(raw, &all); err != nil {
return err
}
all[key]["refresh"] = refresh
raw, _ = json.Marshal(all)
return os.WriteFile(AuthPath(), raw, 0o600)
}
// TestRenewOtherHost is the second magpie of TestRenewOnceAcrossHosts:
// its host starts, it says "ready", and on "go" it sends a request on the
// account, says "renewing" once its host is renewing the sign-in, and
// "said …" for whatever its host says of the sign-in.
func TestRenewOtherHost(t *testing.T) {
account := os.Getenv("MAGPIE_RENEW_OTHER")
if account == "" {
t.Skip("run by TestRenewOnceAcrossHosts")
}
t.Cleanup(Settle)
var mu sync.Mutex
out := func(s string) { mu.Lock(); os.Stdout.WriteString(s + "\n"); mu.Unlock() }
OnSignIn(func(_, _, s string) { out("said " + s) })
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
h, err := get(ctx)
if err != nil {
t.Fatal(err)
}
out("ready")
if line, _ := bufio.NewReader(os.Stdin).ReadString('\n'); line != "go\n" {
t.Fatalf("told %q", line)
}
sent := make(chan error, 1)
go func() { sent <- raceSend(ctx, account) }()
// renewing, or done already: a host that didn't wait for the other's
// renewal, which the first magpie's checks then find
for h.renewing.Load() == 0 && len(sent) == 0 {
time.Sleep(5 * time.Millisecond)
}
out("renewing")
if err := <-sent; err != nil {
t.Fatal(err)
}
Told()
out("sent")
}
// plugins #331: each magpie process runs a host of its own, and two hosts
// renewing one sign-in at once each spent its refresh token, so a vendor
// that spends one once turned the second away and the account was marked
// signed out under the token the first had saved. Two hosts, two
// processes, one store: the first exchange is held until the second host
// is renewing too. The token is exchanged once, neither says the sign-in
// expired, and both send the new token.
func TestRenewOnceAcrossHosts(t *testing.T) {
sandbox(t)
v := newRaceVendor(t)
v.hold = make(chan struct{})
release := sync.OnceFunc(func() { close(v.hold) })
t.Cleanup(release)
var mu sync.Mutex
var said []string
OnSignIn(func(_, _, s string) { mu.Lock(); said = append(said, s); mu.Unlock() })
t.Cleanup(func() { OnSignIn(nil) })
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
key := raceAccount(t, ctx, "r1")
other := exec.Command(os.Args[0], "-test.run=^TestRenewOtherHost$", "-test.count=1", "-test.v")
other.Env = append(os.Environ(), "MAGPIE_RENEW_OTHER="+key)
stdin, _ := other.StdinPipe()
stdout, _ := other.StdoutPipe()
other.Stderr = os.Stderr
if err := other.Start(); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { other.Process.Kill(); other.Wait() })
lines := make(chan string, 64)
go func() {
sc := bufio.NewScanner(stdout)
for sc.Scan() {
lines <- sc.Text()
}
close(lines)
}()
var otherSaid []string
expect := func(want string) {
t.Helper()
for l := range lines {
if l == want {
return
}
if s, ok := strings.CutPrefix(l, "said "); ok {
otherSaid = append(otherSaid, s)
}
}
t.Fatalf("the other magpie ended before %q", want)
}
expect("ready")
sent := make(chan error, 1)
go func() { sent <- raceSend(ctx, key) }()
<-v.arrived // this host's exchange, held
io.WriteString(stdin, "go\n")
expect("renewing")
release()
if err := <-sent; err != nil {
t.Fatal(err)
}
expect("sent")
if err := other.Wait(); err != nil {
t.Fatalf("the other magpie: %v", err)
}
Told()
exchanges, auths := v.seen()
if len(exchanges) != 1 {
t.Errorf("exchanges = %v, want r1 once", exchanges)
}
mu.Lock()
all := append(append([]string(nil), said...), otherSaid...)
mu.Unlock()
for _, s := range all {
if s == "expired" {
t.Errorf("a host said the sign-in expired: this one %v, the other %v", said, otherSaid)
}
}
if len(auths) != 2 || auths[0] != "Bearer a-r2" || auths[1] != "Bearer a-r2" {
t.Errorf("requests went with %v, want the new token twice", auths)
}
if a := raceStored(key); a["refresh"] != "r2" || a["access"] != "a-r2" {
t.Errorf("kept %v", a)
}
}
// A renewal lock left by a magpie that died is taken over at once: the
// renewal is made inside the wait a request gives it, not after the
// lock's minute. A token the vendor turns away still says the sign-in
// expired, and leaves it as it was; one turned away because another
// magpie spent it and saved the new one meanwhile doesn't.
func TestRenewLockOfDeadHostAndRefusal(t *testing.T) {
sandbox(t)
v := newRaceVendor(t)
said := make(chan string, 16)
OnSignIn(func(_, _, s string) { said <- s })
t.Cleanup(func() { OnSignIn(nil) })
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
key := raceAccount(t, ctx, "r1")
dead := exec.Command(os.Args[0], "-test.run=^$")
if err := dead.Run(); err != nil {
t.Fatal(err)
}
sum := sha256.Sum256([]byte(key))
lock := AuthPath() + ".renew-" + hex.EncodeToString(sum[:])[:16] + ".lock"
held := fmt.Sprintf(`{"pid":%d,"at":%d}`, dead.Process.Pid, time.Now().UnixMilli())
if err := os.WriteFile(lock, []byte(held), 0o600); err != nil {
t.Fatal(err)
}
if err := raceSend(ctx, key); err != nil {
t.Fatal(err)
}
if s := waitSaid(t, said); s != "renewed" {
t.Fatalf("said %q", s)
}
if exchanges, auths := v.seen(); len(exchanges) != 1 || len(auths) != 1 || auths[0] != "Bearer a-r2" {
t.Fatalf("exchanges %v, requests %v: the dead magpie's lock was waited out", exchanges, auths)
}
if _, err := os.Stat(lock); !os.IsNotExist(err) {
t.Fatalf("the lock is left: %v", err)
}
refused, err := Import(ctx, "raceco", map[string]any{"type": "oauth", "refresh": "r-never", "access": "a-old", "expires": time.Now().Add(time.Minute).UnixMilli(), "accountId": "refused@race"})
if err != nil {
t.Fatal(err)
}
if err := raceSend(ctx, refused); err != nil {
t.Fatal(err)
}
if s := waitSaid(t, said); s != "expired" {
t.Fatalf("a refused token said %q, want expired", s)
}
if a := raceStored(refused); a["refresh"] != "r-never" || a["access"] != "a-old" {
t.Fatalf("a refused account was changed: %v", a)
}
// turned away because another magpie, one that didn't take the lock,
// spent the token and saved what it got: not signed out, and its
// token is the one sent
raced, err := Import(ctx, "raceco", map[string]any{"type": "oauth", "refresh": "r-raced", "access": "a-old", "expires": time.Now().Add(time.Minute).UnixMilli(), "accountId": "raced@race"})
if err != nil {
t.Fatal(err)
}
if err := changeStored(raced, "r-raced "+raced); err != nil {
t.Fatal(err)
}
if err := raceSend(ctx, raced); err != nil {
t.Fatal(err)
}
if err := raceSend(ctx, raced); err != nil {
t.Fatal(err)
}
Told()
select {
case s := <-said:
t.Fatalf("a token another magpie renewed meanwhile said %q", s)
default:
}
if _, auths := v.seen(); auths[len(auths)-1] != "Bearer a-elsewhere" {
t.Fatalf("requests went with %v, want the other magpie's token last", auths)
}
}