ac4af582301a4e8a29245dbba767eba31334ddf9
2
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
3dc25a6168 |
tests: a plugin fake's vendor counts only its own test's requests (#3357)
Run 39913278700 at |
||
|
|
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 |