Commit Graph
2 Commits
Author SHA1 Message Date
yetoneandvincent 363adb3528 plugin: the auth-lock test checks the loop ran while the lock was held, not how often a timer ticked (#5271)
TestAuthLockWaitDoesNotFreezeTheHost counted a 20ms setInterval over a
300ms sleep and wanted 8 ticks. A loaded runner fires the timer late and
counts fewer while the loop is free all the same: 7 of 8 on mote (#5271),
6 on ubuntu CI at f28787b39 (run 43328831902). It measured the scheduler,
not the freeze it is named for.

Ported from skygunner's fork commit 94d8550c8 (on #5271): a setImmediate
registered before changeAuth checks, from the event loop itself, that the
other host's lock is still there and the change not yet written, then lets
the lock go; the test awaits that and the change. A host that waits with
Atomics.wait (#5112) never reaches the check while the lock is held. The
300/600ms sleeps, the tick count and the 5s bound go; the 20 concurrent
changes and the stale-lock takeover are kept as they were.

Run at this tree (origin/main 1e2ce2463 plus this):
- Linux (wsl-run): go vet ./internal/plugin; -count=20 -run of the test: ok.
- macOS (mote-run): the same, ok.
- gofmt -l: nothing.

Broken by hand on Linux: host.js's sleep made a blocking Atomics.wait. The
test went red at 10.1s, "the event loop ran while the other host still
held the lock"; host.js put back.

Not run: the full suite, and Windows.

Co-authored-by: vincent <webmaster@palmit.com.au>
2026-11-13 08:52:12 +08:00
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