plugin host: the sign-in lock and file waits no longer freeze the event loop (#5112)

pause() was Atomics.wait, which blocks Bun's one thread. lockAuth spun on
plugin-auth.json.lock with pause(5) for up to 2 x AUTH_LOCK_STALE (20s),
and writeDurable and readFileSteady waited up to 500ms each, so one
plugin's renewal storm stalled every plugin's requests, model lists and
check-ins (499s while the port still answered). Reported by mycf_711.

- pause() is gone; sleep() is an awaited setTimeout.
- lockAuth, writeDurable, keepAuth, writeAuth and changeAuth are async,
  and so are their callers (setAuth, removeAuth, settle, save, apiKey,
  staleAccess, import/take/signOut, pi.js modify/delete).
- changeAuth takes in-process changes one at a time (authQueue) before
  the file lock, so a host's own changes don't poll against each other;
  the lock poll backs off 5ms to 50ms. Stale-lock takeover (10s) and the
  change being made on a fresh read under the lock are unchanged.
- The sync readFileSteady (readAuth, hot path) no longer waits: it retries
  twice at once, and readAuth keeps the last good read when the OS still
  refuses (a failed read is unknown, not none). Under the lock the wait
  is async (readFileSteadyAsync, 50 x 10ms as before).

Test: TestAuthLockWaitDoesNotFreezeTheHost holds the lock 600ms and checks
a timer keeps ticking while changeAuth waits, 20 concurrent changes lose
none, and a lock 11s old is taken over. On the old host.js it fails
("the lock is still held at 300ms": the loop froze 10s). Verified by
putting the old host.js and pi.js back.

Run (mote, at this tree): go test ./internal/plugin ./internal/gateway
./internal/provider all ok; gofmt, GOOS=windows/linux go vet of
internal/plugin clean. Not run: full go test ./..., a real Windows host
under a renewal storm. The serial-fallback total budget is #5113.
This commit is contained in:
yetone
2026-11-04 01:53:46 +08:00
parent e4bc3eeb6f
commit 7aff42d863
3 changed files with 190 additions and 60 deletions
+88
View File
@@ -0,0 +1,88 @@
package plugin
import (
"bytes"
"context"
"os/exec"
"path/filepath"
"testing"
"time"
)
// TestAuthLockWaitDoesNotFreezeTheHost (#5112): a plugin-auth.json.lock held
// by another host makes changeAuth wait, and the wait must leave the event
// loop free: every other plugin's timers, requests and model lists run on
// the same loop. Atomics.wait in pause() froze it for as long as the lock
// was held (up to 20s). The change is still made once the lock is let go,
// concurrent changes in one host don't lose each other's, and a lock
// left by a dead host (older than AUTH_LOCK_STALE) is taken over.
func TestAuthLockWaitDoesNotFreezeTheHost(t *testing.T) {
node, err := exec.LookPath("node")
if err != nil {
t.Skip("no node on PATH")
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
cmd := exec.CommandContext(ctx, node, "-e", authLockAsyncJS, filepath.Join(t.TempDir(), "plugin-auth.json"))
cmd.Stdin = bytes.NewReader(hostJS)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("%v\n%s", err, out)
}
}
const authLockAsyncJS = `
const assert = require('node:assert/strict')
const realFs = require('node:fs')
const path = require('node:path')
const vm = require('node:vm')
const source = realFs.readFileSync(0, 'utf8')
const start = source.indexOf('function parseAuth(')
const end = source.indexOf('// ---- accounts', start)
assert.ok(start >= 0 && end > start, 'production section exists')
const authPath = process.argv[1]
const context = vm.createContext({
fs: realFs, path, authPath, Buffer, structuredClone, Date, Promise, Math, JSON, Object, Error, Array, String, Number,
setTimeout, process, toErr: () => {}, send: () => {},
})
vm.runInContext(source.slice(start, end), context)
const run = code => vm.runInContext(code, context)
const lock = authPath + '.lock'
const sleep = ms => new Promise(r => setTimeout(r, ms))
;(async () => {
realFs.writeFileSync(authPath, JSON.stringify({ a: { type: 'api', key: 'k1' } }))
// another host holds the lock for 600ms
realFs.writeFileSync(lock, '')
let ticks = 0
const timer = setInterval(() => ticks++, 20)
let released = false
setTimeout(() => { released = true; try { realFs.unlinkSync(lock) } catch {} }, 600)
const t0 = Date.now()
const waiting = run('changeAuth((all) => { all.b = { type: "api", key: "k2" } })')
await sleep(300)
assert.ok(!released, 'the lock is still held at 300ms')
assert.ok(ticks >= 8, 'a timer on the same loop kept running while changeAuth waited for the lock: ' + ticks + ' ticks')
await waiting
assert.ok(released, 'the change waited for the lock to be let go')
assert.ok(Date.now() - t0 < 5000, 'and did not wait out the stale limit')
clearInterval(timer)
const now = JSON.parse(realFs.readFileSync(authPath, 'utf8'))
assert.equal(now.b.key, 'k2')
assert.equal(now.a.key, 'k1', 'the others are kept')
assert.ok(!realFs.existsSync(lock), 'the lock is let go')
// 20 changes at once lose none
await Promise.all(Array.from({ length: 20 }, (_, i) => run('changeAuth((all) => { all.n = { n: (all.n?.n ?? 0) + 1 } })')))
assert.equal(JSON.parse(realFs.readFileSync(authPath, 'utf8')).n.n, 20, 'no two writers at once')
// a lock left by a host that died
realFs.writeFileSync(lock, '')
const old = new Date(Date.now() - 11e3)
realFs.utimesSync(lock, old, old)
await run('changeAuth((all) => { all.c = { type: "api", key: "k3" } })')
assert.equal(JSON.parse(realFs.readFileSync(authPath, 'utf8')).c.key, 'k3', 'a stale lock is taken over')
assert.ok(!realFs.existsSync(lock))
console.log('ok')
})().catch(e => { console.error(e); process.exit(1) })
`
+100 -58
View File
@@ -764,16 +764,30 @@ function parseAuth(text) {
}
}
// readFileSteady reads p; a read the OS refuses for a moment (Windows,
// while the file is being renamed over) is tried again at once and then
// thrown, never read as none: the next setAuth would write the others'
// accounts away. It never waits: the event loop is every plugin's (#5112),
// so a caller that can wait uses readFileSteadyAsync.
function readFileSteady(p) {
for (let i = 0; ; i++) {
try {
return fs.readFileSync(p)
} catch (e) {
if (e?.code === "ENOENT") return null
// Windows refuses a file being renamed over for a moment: read as
// none, the next setAuth would write the others' accounts away
if (i >= 2) throw e
}
}
}
async function readFileSteadyAsync(p) {
for (let i = 0; ; i++) {
try {
return fs.readFileSync(p)
} catch (e) {
if (e?.code === "ENOENT") return null
if (i >= 50) throw e
pause(10)
await sleep(10)
}
}
}
@@ -792,6 +806,14 @@ let authUnread = false
// there is none, known; one that doesn't read is its .bak's, known, or
// none, unknown.
function readAuthState(buf = readFileSteady(authPath)) {
return authStateOf(buf)
}
async function readAuthStateAsync() {
return authStateOf(await readFileSteadyAsync(authPath))
}
function authStateOf(buf) {
if (buf === null) return { all: {}, known: true }
const v = parseAuth(buf.toString("utf8"))
if (v) return { all: v, known: true, own: true }
@@ -834,7 +856,14 @@ function readAuth() {
const e = authSeen
const same = st && e && e.mtimeMs === st.mtimeMs && e.size === st.size && e.ino === st.ino
if (same && !e.raw) return e.all
const buf = readFileSteady(authPath)
let buf
try {
buf = readFileSteady(authPath)
} catch (err) {
// refused for a moment: the last good read stands, not none
if (e) return e.all
throw err
}
if (same && buf && buf.equals(e.raw)) {
if (Date.now() - e.mtimeMs > AUTH_SETTLED) e.raw = null
return e.all
@@ -857,7 +886,7 @@ function authCopy(key) {
// schedule, the data behind it later, and a machine that went down between
// the two left plugin-auth.json all zero (#1505). A write or flush that
// fails leaves p as it was.
function writeDurable(p, data) {
async function writeDurable(p, data) {
const tmp = p + ".tmp-" + process.pid
try {
const f = fs.openSync(tmp, "w", 0o600)
@@ -873,7 +902,7 @@ function writeDurable(p, data) {
return fs.renameSync(tmp, p)
} catch (e) {
if (i >= 50 || !["EPERM", "EACCES", "EBUSY"].includes(e?.code)) throw e
pause(10)
await sleep(10)
}
}
} catch (e) {
@@ -929,8 +958,8 @@ function keepBad(buf) {
// keepAuth is called before plugin-auth.json is replaced: the file there
// now becomes plugin-auth.json.bak when it reads, or is copied aside when
// it doesn't. A failure stops the write.
function keepAuth() {
const buf = readFileSteady(authPath)
async function keepAuth() {
const buf = await readFileSteadyAsync(authPath)
if (buf === null) return
if (!parseAuth(buf.toString("utf8"))) {
keepBad(buf)
@@ -941,30 +970,35 @@ function keepAuth() {
try {
old = fs.readFileSync(bak)
} catch {}
if (!old || !old.equals(buf)) writeDurable(bak, buf)
if (!old || !old.equals(buf)) await writeDurable(bak, buf)
}
function writeAuth(all) {
async function writeAuth(all) {
fs.mkdirSync(path.dirname(authPath), { recursive: true })
keepAuth()
writeDurable(authPath, Buffer.from(JSON.stringify(all, null, 2) + "\n"))
await keepAuth()
await writeDurable(authPath, Buffer.from(JSON.stringify(all, null, 2) + "\n"))
}
function pause(ms) {
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms)
}
// sleep waits ms without holding the event loop. Nothing in the host waits
// any other way: Atomics.wait here froze every plugin's requests, model
// lists and check-ins for as long as one of them spun on the lock (#5112).
const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
// Two hosts run at once while one is restarted (the old one finishing its
// calls), and magpie writes the file when no host runs: each change to it
// is made under plugin-auth.json.lock, read afresh and written back with
// only its own accounts changed, so a host never writes another's newer
// token away with what it read before. Nothing awaits under the lock; one
// held longer than AUTH_LOCK_STALE was left by a host that died.
// token away with what it read before. The change itself is synchronous;
// the lock is waited for, and the write made, without holding the event
// loop. A lock held longer than AUTH_LOCK_STALE was left by a host that
// died. Changes within this host take the file lock one at a time
// (authQueue), so they don't poll against each other.
const AUTH_LOCK_STALE = 10 * 1000
function lockAuth() {
async function lockAuth() {
const lock = authPath + ".lock"
fs.mkdirSync(path.dirname(authPath), { recursive: true })
let tries = 0
for (const start = Date.now(); ; ) {
try {
fs.closeSync(fs.openSync(lock, "wx", 0o600))
@@ -985,36 +1019,44 @@ function lockAuth() {
fs.unlinkSync(lock)
} catch {}
}
pause(5)
await sleep(Math.min(50, 5 + 5 * tries++))
}
}
// changeAuth runs change on the file as it is now, under the lock, and
// writes it back when change says it changed it.
let authQueue = Promise.resolve()
function changeAuth(change) {
const unlock = lockAuth()
try {
const { all, known } = readAuthState()
const out = change(all)
// sign-ins that don't read are never written over with none: a
// sign-out of one of them waits for the file to be put right
if (out !== false && (known || Object.keys(all).length > 0)) writeAuth(all)
return out
} finally {
unlock()
}
const run = authQueue.then(async () => {
const unlock = await lockAuth()
try {
const { all, known } = await readAuthStateAsync()
const out = change(all)
// sign-ins that don't read are never written over with none: a
// sign-out of one of them waits for the file to be put right
if (out !== false && (known || Object.keys(all).length > 0)) await writeAuth(all)
return out
} finally {
unlock()
}
})
authQueue = run.then(
() => {},
() => {},
)
return run
}
function setAuth(key, info) {
changeAuth((all) => {
async function setAuth(key, info) {
await changeAuth((all) => {
all[key] = info
})
forgetAccount(key)
send({ event: "auth", provider: providerOf(key), account: key })
}
function removeAuth(key) {
changeAuth((all) => {
async function removeAuth(key) {
await changeAuth((all) => {
delete all[key]
})
forgetAccount(key)
@@ -1101,8 +1143,8 @@ function renamedBy(now, was) {
// replaces that one's and goes. It gives where it is kept. An account
// named another way before keeps its account id, which the user's name
// for it, its proxy and gateway keys are kept by.
function settle(provider, key) {
const k = changeAuth((all) => {
async function settle(provider, key) {
const k = await changeAuth((all) => {
const now = all[key]
if (!now) return false
const who = whoOf(now)
@@ -1155,7 +1197,7 @@ function makeClient() {
// tokens over the old; nothing is merged. It goes to the
// account the request is for.
const key = keyFor(id)
const kept = changeAuth((all) => {
const kept = await changeAuth((all) => {
if (JSON.stringify(all[key]) === JSON.stringify(body)) return false
all[key] = body
})
@@ -1168,7 +1210,7 @@ function makeClient() {
},
remove: async (opts) => {
const id = opts?.path?.id ?? opts?.id
if (typeof id === "string") removeAuth(keyFor(id))
if (typeof id === "string") await removeAuth(keyFor(id))
return { data: true }
},
},
@@ -2294,17 +2336,17 @@ function validate(provider, index, key, value) {
// save keeps a successful sign-in as OpenCode's CLI does, at key (or, for
// a sign-in the plugin says is another provider's, a new account of
// that one). It gives the provider and where the account is kept.
function save(provider, key, result, inputs, apiKey) {
async function save(provider, key, result, inputs, apiKey) {
const id = result.provider ?? provider
if (id !== provider) key = freshKey(id)
if ("refresh" in result) {
const { type: _t, provider: _p, refresh, access, expires, ...extra } = result
setAuth(key, { type: "oauth", refresh, access, expires, ...extra })
await setAuth(key, { type: "oauth", refresh, access, expires, ...extra })
} else if ("key" in result || apiKey) {
const md = { ...(inputs && Object.keys(inputs).length ? inputs : {}), ...(result.metadata ?? {}) }
setAuth(key, { type: "api", key: result.key ?? apiKey, ...(Object.keys(md).length ? { metadata: md } : {}) })
await setAuth(key, { type: "api", key: result.key ?? apiKey, ...(Object.keys(md).length ? { metadata: md } : {}) })
}
return { provider: id, account: settle(id, key) }
return { provider: id, account: await settle(id, key) }
}
// signInKey is where a sign-in goes: the account named, else a new one.
@@ -2378,7 +2420,7 @@ async function callback({ session, code }, signal) {
const r = await signingIn(signal, s.proxy, s.provider, s.key, () => (s.a.method === "code" ? s.a.callback(code ?? "", { signal }) : s.a.callback(undefined, { signal })))
if (signal?.aborted) throw abortedSignIn()
if (!r || r.type !== "success") return failed(r)
return { ok: true, ...save(s.provider, s.key, r, undefined) }
return { ok: true, ...(await save(s.provider, s.key, r, undefined)) }
}
async function apiKey({ provider, method, inputs, key, account, proxy }, signal) {
@@ -2387,13 +2429,13 @@ async function apiKey({ provider, method, inputs, key, account, proxy }, signal)
const at = signInKey(provider, account)
if (typeof m.authorize !== "function") {
const md = inputs && Object.keys(inputs).length ? { metadata: inputs } : {}
setAuth(at, { type: "api", key, ...md })
return { ok: true, provider, account: settle(provider, at) }
await setAuth(at, { type: "api", key, ...md })
return { ok: true, provider, account: await settle(provider, at) }
}
const r = await signingIn(signal, proxy ?? "", provider, at, () => m.authorize(inputs ?? {}, { signal }))
if (signal?.aborted) throw abortedSignIn()
if (!r || r.type !== "success") return failed(r)
return { ok: true, ...save(provider, at, r, inputs, key) }
return { ok: true, ...(await save(provider, at, r, inputs, key)) }
}
// ---- renewing a sign-in ------------------------------------------------------
@@ -2616,7 +2658,7 @@ async function renewLocked(provider, key, a, was) {
// that one stands. Checked under the lock the save is made under, so
// nothing comes between the two.
const { type: _t, ...fields } = got
const kept = changeAuth((all) => {
const kept = await changeAuth((all) => {
if (changed(all[key], was)) return false
all[key] = { ...was, ...fields, type: "oauth" }
})
@@ -2834,7 +2876,7 @@ async function doFetch(id, record, params) {
// still turned away, or was due and the plugin didn't renew it; a
// refresh the vendor refuses says so itself (signIn "expired"). A
// sign-in that can't be read now is unknown: kept, not marked.
function staleAccess(key, before) {
async function staleAccess(key, before) {
const now = readAuthState()
if (!now.known) return "kept"
if (before?.type !== "oauth" || !before.refresh) return ""
@@ -2842,7 +2884,7 @@ function staleAccess(key, before) {
if (cur?.type !== "oauth" || cur.refresh !== before.refresh || cur.access !== before.access) return ""
const exp = Number(before.expires)
if (!Number.isFinite(exp) || exp <= Date.now()) return ""
changeAuth((all) => {
await changeAuth((all) => {
const a = all[key]
if (a?.type !== "oauth" || a.access !== before.access || a.refresh !== before.refresh) return false
all[key] = { ...a, expires: 1 }
@@ -2911,7 +2953,7 @@ async function fetchAs(id, record, key, params) {
const rh = {}
res.headers.forEach((v, k) => (rh[k] = v))
if (res.status === 401 && !("x-magpie-sign-in" in rh) && key) {
const said = staleAccess(key, before)
const said = await staleAccess(key, before)
if (said) rh["x-magpie-sign-in"] = said
}
const head = headCost(rh)
@@ -3022,20 +3064,20 @@ const handlers = {
// onto its plugin) as one more account, or as the account it already is
// import keeps a sign-in made elsewhere as one of provider's accounts;
// with a key, as that account again (one taken back)
import(p) {
async import(p) {
if (p.key && providerOf(p.key) === p.provider) {
setAuth(p.key, p.auth)
await setAuth(p.key, p.auth)
return { account: p.key }
}
const key = freshKey(p.provider)
setAuth(key, p.auth)
return { account: settle(p.provider, key) }
await setAuth(key, p.auth)
return { account: await settle(p.provider, key) }
},
// take gives the accounts named (else every account of the provider) and
// forgets them in one step: nothing renews a token between the two
take(p) {
async take(p) {
const out = {}
changeAuth((all) => {
await changeAuth((all) => {
for (const k of p.accounts?.length ? p.accounts : accountsOf(all, p.provider)) {
if (!(k in all)) continue
out[k] = all[k]
@@ -3050,9 +3092,9 @@ const handlers = {
return { auths: out }
},
// signOut forgets the account named, else every account of the provider
signOut(p) {
async signOut(p) {
const keys = p.account ? [p.account] : accountsOf(readAuth(), p.provider)
for (const k of keys) removeAuth(k)
for (const k of keys) await removeAuth(k)
return null
},
reload(p) {
+2 -2
View File
@@ -536,7 +536,7 @@ export async function load(h, list) {
const was = h.readAuth()[key]
const next = await fn(toPi(was))
if (next === undefined) return toPi(was)
h.changeAuth((all) => {
await h.changeAuth((all) => {
all[key] = fromPi(next, all[key])
})
h.send({ event: "auth", provider: key.split("#")[0], account: key })
@@ -548,7 +548,7 @@ export async function load(h, list) {
},
async delete(id) {
const key = h.keyFor(id)
h.changeAuth((all) => {
await h.changeAuth((all) => {
if (!(key in all)) return false
delete all[key]
})