Files
magpie/internal/gateway/masking_wait_test.go
T
yetone 16f6e568f4 gateway: masking's time no longer counts toward the 2 minutes before routing (#3682)
yunwang2442-creator: a 480 KB request the local masking model takes 90s
over on an RX 470/580 (more on its CPU, and more behind the requests
ahead of it in the model's one turn) was answered 504 at unroutedMost,
2 minutes from its arrival, its masking cut off, and the same again
each time it was sent.

unroutedMost now counts the request's time other than masking: a
masking (noteMasking), its wait for the model's turn with it, is
tracked like the known waits, and most's timer (checkMost) looks again
when the last masking ends, with what is left. unroutedUnknown is as
before.

Reconciled with 5a94eec3a, not taken as the report wrote it: the report
asked for unroutedMost to count only unknown waits. Next to the 30s
unknown bound that is dead code (unknown fires first), and a known wait
that hangs (a plugin's gateway hook, an image description never ending)
would be held for good, the heyusaku6 case. So only masking is left out.
Masking stays bounded inside internal/pii: classifyTimeout per request
to pf-serve, gpuLoadTimeout/loadTimeout per load, and a model that keeps
failing holds requests back at once (heldBack, retryAfter); a client
that leaves lets go of the turn's queue (FindContext).

The masking 504 message no longer says "what the model has already
read is kept, so it goes quicker": a request cut off mid-read had
nothing kept. It is reached now only by a masking begun as the rest of
the wait ran out.

TestHeldForTheMaskingModelSaysSo, which held a request at the model
past unroutedMost and expected the 504, is now
TestMaskingModelWaitNotCutOffAtMost (no answer past 5x unroutedMost;
the model's wait lets go when the client leaves) and
TestUnroutedMsgSaysTheMaskingModel (the message's words).
TestUnroutedMostLeavesOutMasking: masked 3x unroutedMost, not answered;
then held by an image description that never ends, answered 504.

Run: go test -count=1 -run 'Unrouted|Masking|Held|Mask'
./internal/gateway/ ok; go test ./internal/pii/ ok; gofmt -l clean;
GOOS=linux and GOOS=windows go vet of both ok. Full gateway and pii
run before the push, recorded in the last commit of this set.
Broken by hand: checkMost counting from arrival again
(TestMaskingModelWaitNotCutOffAtMost and TestUnroutedMostLeavesOutMasking
red, 504 while masked); checkMost never giving up
(TestUnroutedMostLeavesOutMasking and TestUnroutedRequestShownThenAnswered
red).
Not tried: a real long request through a real pf-serve on a weak GPU.
2026-10-27 01:14:23 +08:00

144 lines
5.2 KiB
Go

package gateway
import (
"context"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/gwmw"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// waitingMasker is masking that waits for the local model, which never
// answers: a pf-serve loading or busy with the requests ahead (#3682).
// let is closed when the test ends; left is closed when a wait let go
// because its request's context ended.
type waitingMasker struct{ let, left chan struct{} }
func (m waitingMasker) wait(ctx context.Context) {
select {
case <-ctx.Done():
close(m.left)
panic("stopped waiting for the local masking model")
case <-m.let:
panic("the test ended")
}
}
func (m waitingMasker) JSONHitsContext(ctx context.Context, b []byte) ([]byte, []gwmw.Hit) {
m.wait(ctx)
return b, nil
}
func (m waitingMasker) TextHitsContext(ctx context.Context, s string) (string, []gwmw.Hit) {
m.wait(ctx)
return s, nil
}
func (m waitingMasker) JSONHits(b []byte) ([]byte, []gwmw.Hit) {
return m.JSONHitsContext(context.Background(), b)
}
func (m waitingMasker) TextHits(s string) (string, []gwmw.Hit) {
return m.TextHitsContext(context.Background(), s)
}
func (m waitingMasker) JSON(b []byte) ([]byte, int) { b, _ = m.JSONHits(b); return b, 0 }
func (m waitingMasker) Text(s string) (string, int) { s, _ = m.TextHits(s); return s, 0 }
func (waitingMasker) Restore(s string, _ bool) string { return s }
func (waitingMasker) Value(string) (string, bool) { return "", false }
func (waitingMasker) Writer(w http.ResponseWriter) (http.ResponseWriter, func()) {
return w, func() {}
}
// #3682: with Settings → Privacy's local model on, a request the model
// was still reading at 2 minutes was answered 504, its work thrown away,
// and so every time it was sent again (480 KB on an RX 470/580 takes the
// model 90s, and its turn waits for the requests ahead). Masking's time no
// longer counts toward unroutedMost: the request waits for the model,
// and its wait lets go when its client does, rather than keep its place
// in the model's queue.
func TestMaskingModelWaitNotCutOffAtMost(t *testing.T) {
unroutedLimits(t, 20*time.Millisecond, 200*time.Millisecond)
fresh(t)
if err := settings.Save(settings.Settings{Redact: true}); err != nil {
t.Fatal(err)
}
if err := provider.Save(provider.Provider{ID: "probe", Name: "Probe", Chat: "http://127.0.0.1:9/v1", Key: "key", Models: []string{"text"}}); err != nil {
t.Fatal(err)
}
on := modelOn
modelOn = func() bool { return true }
t.Cleanup(func() { modelOn = on })
withoutMasker(t)
m := waitingMasker{let: make(chan struct{}), left: make(chan struct{})}
t.Cleanup(func() { close(m.let) })
remove := gwmw.Mask(m)
t.Cleanup(remove)
s := New()
srv := httptest.NewServer(s.Handler())
t.Cleanup(srv.Close)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
got := make(chan unroutedReply, 1)
go func() {
req, _ := http.NewRequestWithContext(ctx, "POST", srv.URL+"/v1/chat/completions", strings.NewReader(`{"model":"probe/text","messages":[{"role":"user","content":"hi"}]}`))
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
got <- unroutedReply{err: err}
return
}
b, _ := io.ReadAll(resp.Body)
resp.Body.Close()
got <- unroutedReply{code: resp.StatusCode, body: string(b)}
}()
u := watchedHold(t, s)
if r := heldPast(t, u, 5*unroutedMost, got); r != nil {
t.Fatalf("a request waiting for the masking model was answered %d %q %v past unroutedMost", r.code, r.body, r.err)
}
if u.masking.Load() == 0 {
t.Fatal("held past unroutedMost, but not while masking")
}
cancel() // the client goes away
select {
case <-m.left:
case <-time.After(10 * time.Second):
t.Fatal("the request's wait for the model went on after its client went away")
}
}
// A masking begun as the rest of a request's wait ran out still says it
// waited for the local masking model, what the model was doing and how to
// send without it; and no longer that what the model read is kept, which
// held only for texts it had finished.
func TestUnroutedMsgSaysTheMaskingModel(t *testing.T) {
on, busy := modelOn, modelBusy
modelOn, modelBusy = func() bool { return true }, func() string { return "it is still loading" }
t.Cleanup(func() { modelOn, modelBusy = on, busy })
m := unroutedMsg(2*time.Minute, true)
if !strings.Contains(m, "waiting for the local masking model") || !strings.Contains(m, "it is still loading") ||
!strings.Contains(m, "turn it off in Settings") || strings.Contains(m, "without routing") || strings.Contains(m, "kept") {
t.Fatalf("held for the model: %s", m)
}
}
// With the model off, a request held while masked says it was held while
// masking, not that it waited for a model that isn't on.
func TestUnroutedMsgNamesTheModelOnlyWhenItIsOn(t *testing.T) {
on := modelOn
modelOn = func() bool { return false }
t.Cleanup(func() { modelOn = on })
if m := unroutedMsg(2*time.Minute, true); strings.Contains(m, "model") || !strings.Contains(m, "while masking it") {
t.Fatalf("model off: %s", m)
}
if m := unroutedMsg(2*time.Minute, false); !strings.Contains(m, "without routing it to any provider") {
t.Fatalf("not masking: %s", m)
}
}