shualaitianduai on Discord: a plugin whose sign-in kept failing (401)
held up the plugins behind it. host.js loaded the plugins one after
another and answered init only when all were in, so a plugin function
that never returned held every other plugin until init's two-minute
deadline failed the whole host. providers() listed the providers one
after another too, so one stuck models hook held the whole list past
refreshDeadline (30s), and every provider's list went stale.
Now loadPlugins loads every plugin side by side, each given
pluginLoadWait (20s) for its import and plugin function, and each
config hook as long; one still loading is told as not loaded ("the
plugin's load didn't finish in 20s") and init answers with the rest.
Hooks are kept in the list's order, as before. providers() lists every
provider side by side and gives each models hook pluginListWait (20s,
inside refreshDeadline): one that hasn't answered is listed as fallen
back with that as its listError, so magpie keeps the list it had, and
that answer isn't kept for requests (keepInfo). Documented in
docs/subsystems/provider-plugins.md.
Run at this head (base origin/main e5dd86965), sandbox HOME, caches
pinned:
- TestStuckPluginLoadHoldsNoOther and TestStuckModelListHoldsNoOther
(testdata/stuck): red on origin/main's host.js ("Plugins = [],
starting plugins: context deadline exceeded" after 30s; "Providers
= [], context deadline exceeded" after 15s), green here (2.1s each).
- go test -count=1 -timeout 45m ./internal/plugin/: ok (165s).
- go test ./internal/provider/: ok with
-skip TestUnreadLoginsWriteErrorsKeepCredentials, which crashes with
"close of synctest channel from outside bubble" in filememo.Forget,
filed as #3632 and not touched here.
- go test -run Plugin ./internal/gateway/: ok.
- gofmt -l clean; go vet, GOOS=linux and GOOS=windows go vet -tags
nogui on internal/plugin: ok.
Not run before the push: the whole go test ./... (run after it); not
tried with the reporter's own plugin, which wasn't named.
104 lines
3.3 KiB
Go
104 lines
3.3 KiB
Go
package plugin
|
|
|
|
import (
|
|
"context"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// stuckWaits shortens how long the host waits on one plugin's load and on
|
|
// one provider's model list, for the test.
|
|
func stuckWaits(t *testing.T) {
|
|
t.Helper()
|
|
load, list := pluginLoadWait, pluginListWait
|
|
pluginLoadWait, pluginListWait = 2*time.Second, 2*time.Second
|
|
t.Cleanup(func() { pluginLoadWait, pluginListWait = load, list })
|
|
}
|
|
|
|
// addStuck adds the stuck plugin ahead of the fake one, so the host meets
|
|
// it first, as a plugin earlier in the user's list.
|
|
func addStuck(t *testing.T, ctx context.Context) {
|
|
t.Helper()
|
|
for _, p := range []string{"testdata/stuck/index.js", "testdata/fake/index.js"} {
|
|
abs, _ := filepath.Abs(p)
|
|
if _, err := Add(ctx, abs); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A plugin whose load never finishes, as one whose sign-in is refused and
|
|
// tried again, holds up none of the plugins after it: they load and are
|
|
// listed, and it is told as not loaded, saying why (shualaitianduai on
|
|
// Discord). Before, the host loaded the plugins one after another and
|
|
// answered init only when all were in, so every plugin waited until
|
|
// magpie's two-minute deadline failed the whole host.
|
|
func TestStuckPluginLoadHoldsNoOther(t *testing.T) {
|
|
sandbox(t)
|
|
stuckWaits(t)
|
|
t.Setenv("STUCK_LOAD", "1")
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
addStuck(t, ctx)
|
|
|
|
ls, err := Plugins(ctx)
|
|
if err != nil || len(ls) != 2 {
|
|
t.Fatalf("Plugins = %+v, %v", ls, err)
|
|
}
|
|
for _, l := range ls {
|
|
stuck := strings.Contains(l.Spec, "stuck")
|
|
if stuck && !strings.Contains(l.Error, "didn't finish") {
|
|
t.Errorf("the stuck plugin says %q, not that it didn't finish loading", l.Error)
|
|
}
|
|
if !stuck && l.Error != "" {
|
|
t.Errorf("the plugin after the stuck one didn't load: %s", l.Error)
|
|
}
|
|
}
|
|
ps, err := Providers(ctx)
|
|
if err != nil || len(ps) != 1 || ps[0].ID != "fakeco" || len(ps[0].Models) == 0 {
|
|
t.Fatalf("Providers = %+v, %v; want fakeco with its models", ps, err)
|
|
}
|
|
}
|
|
|
|
// A provider whose models hook never answers holds up no other provider's
|
|
// listing: the list comes back within the host's wait, the other provider
|
|
// with its models and the stuck one with a list error saying it gave up.
|
|
// Before, the host listed the providers one after another, and the whole
|
|
// list waited on the stuck one past refreshDeadline.
|
|
func TestStuckModelListHoldsNoOther(t *testing.T) {
|
|
sandbox(t)
|
|
stuckWaits(t)
|
|
t.Setenv("STUCK_MODELS", "1")
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
addStuck(t, ctx)
|
|
for _, id := range []string{"stuckco", "fakeco"} {
|
|
if _, err := APIKey(ctx, id, 0, nil, "k-"+id, NewAccount); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
lctx, lcancel := context.WithTimeout(ctx, 15*time.Second)
|
|
defer lcancel()
|
|
ps, err := Providers(lctx)
|
|
if err != nil || len(ps) != 2 {
|
|
t.Fatalf("Providers = %+v, %v", ps, err)
|
|
}
|
|
for _, p := range ps {
|
|
switch p.ID {
|
|
case "fakeco":
|
|
if p.FellBack || len(p.Models) == 0 {
|
|
t.Errorf("fakeco listed fellBack=%v with %d models, %q", p.FellBack, len(p.Models), p.ListError)
|
|
}
|
|
case "stuckco":
|
|
if !p.FellBack || !strings.Contains(p.ListError, "didn't finish") {
|
|
t.Errorf("stuckco listed fellBack=%v saying %q", p.FellBack, p.ListError)
|
|
}
|
|
default:
|
|
t.Errorf("listed %q", p.ID)
|
|
}
|
|
}
|
|
}
|