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).