Files
magpie/internal/plugin/auth_lock_async_test.go
T
yetone 7aff42d863 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.
2026-11-04 01:53:46 +08:00

89 lines
3.6 KiB
Go

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) })
`