mirror of
https://github.com/mudler/LocalAI.git
synced 2026-07-30 18:09:05 -04:00
fix(distributed): reject wrong-model requests at the backend (#10952) In distributed mode the controller caches a NodeModel row naming a backend's host:port. A worker can recycle a stopped backend's gRPC port for a different model's backend, and probeHealth verifies liveness rather than identity, so the probe succeeds against whatever now occupies the port and the request is dispatched to the wrong backend. The caller gets a silent wrong-model answer. Nothing in the request could catch this: PredictOptions had no model field, so model identity crossed the wire only in ModelOptions.Model at LoadModel time, and the cached-hit path issues no LoadModel. Every backend's "model not loaded" guard checks a nil handle, which a process holding a different model passes, so the stale row was never dropped either. Add PredictOptions.ModelIdentity and enforce it at the point of use: - The controller populates it in gRPCPredictOpts from ModelConfig.Model, the same expression ModelOptions feeds to model.WithModel and therefore the same value the backend received as ModelOptions.Model. Both are read from one config value in one function, so they are equal by construction and the comparison cannot false-reject. - Backends compare it against what they loaded and return NOT_FOUND with a fixed sentinel. Enforced in pkg/grpc/server.go (27 Go backends), an interceptor in backend/python/common (all 36 Python backends, no per-backend change), and the llama-cpp / ik-llama-cpp / ds4 C++ servers. That is every backend with real exposure: kokoros answers all four RPCs with unimplemented and privacy-filter implements none of them. - The router's reconcile drops the stale replica row on a mismatch, so the next request reloads somewhere correct. Empty means "skip the check" on both sides: a controller that predates the field sends nothing, a backend loaded by such a controller has nothing to compare, and the C++ server synthesizes PredictOptions internally for ASR. That keeps upgrades working in both directions. Scoped to the four PredictOptions RPCs. TTSRequest.model and SoundGenerationRequest.model are deliberately NOT validated: FileStagingClient already rewrites them to worker-local absolute paths, so in distributed mode they already differ from the load-time value and comparing them would reject valid requests. IsModelMismatch requires both the NOT_FOUND code and the sentinel, unlike the neighbouring helpers which accept either. insightface's Embedding returns NOT_FOUND "no face detected" on a PredictOptions RPC, and a code-only check would drop a healthy replica row on every faceless image. Assisted-by: Claude Code:claude-opus-4-8[1m] [Read] [Edit] [Bash] Signed-off-by: Ettore Di Giacinto <mudler@localai.io> Co-authored-by: Ettore Di Giacinto <mudler@localai.io>
129 lines
3.9 KiB
Go
129 lines
3.9 KiB
Go
package grpc
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/mudler/LocalAI/pkg/grpc/base"
|
|
"github.com/mudler/LocalAI/pkg/grpc/grpcerrors"
|
|
pb "github.com/mudler/LocalAI/pkg/grpc/proto"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
// identityBackend records what it was loaded with and answers every inference
|
|
// RPC successfully. Any request that reaches it has passed the identity guard,
|
|
// so `served` is the signal for "the guard let this through".
|
|
type identityBackend struct {
|
|
base.SingleThread
|
|
|
|
loaded string
|
|
served int
|
|
}
|
|
|
|
func (b *identityBackend) Load(opts *pb.ModelOptions) error {
|
|
b.loaded = opts.Model
|
|
return nil
|
|
}
|
|
|
|
func (b *identityBackend) Predict(*pb.PredictOptions) (string, error) {
|
|
b.served++
|
|
return "ok", nil
|
|
}
|
|
|
|
func (b *identityBackend) PredictStream(_ *pb.PredictOptions, ch chan string) error {
|
|
b.served++
|
|
ch <- "ok"
|
|
close(ch)
|
|
return nil
|
|
}
|
|
|
|
func (b *identityBackend) Embeddings(*pb.PredictOptions) ([]float32, error) {
|
|
b.served++
|
|
return []float32{1}, nil
|
|
}
|
|
|
|
func (b *identityBackend) TokenizeString(*pb.PredictOptions) (pb.TokenizationResponse, error) {
|
|
b.served++
|
|
return pb.TokenizationResponse{Length: 1}, nil
|
|
}
|
|
|
|
var _ AIModel = (*identityBackend)(nil)
|
|
|
|
// callAll exercises the four PredictOptions RPCs and returns the first error.
|
|
// All four share one guard, so all four must behave identically.
|
|
func callAll(c Backend, in *pb.PredictOptions) []error {
|
|
ctx := context.Background()
|
|
errs := []error{}
|
|
|
|
_, err := c.Predict(ctx, in)
|
|
errs = append(errs, err)
|
|
|
|
errs = append(errs, c.PredictStream(ctx, in, func(*pb.Reply) {}))
|
|
|
|
_, err = c.Embeddings(ctx, in)
|
|
errs = append(errs, err)
|
|
|
|
_, err = c.TokenizeString(ctx, in)
|
|
errs = append(errs, err)
|
|
|
|
return errs
|
|
}
|
|
|
|
var _ = Describe("PredictOptions model identity guard", func() {
|
|
newServed := func(addr, loadedModel string) (Backend, *identityBackend) {
|
|
b := &identityBackend{}
|
|
Provide(addr, b)
|
|
c := NewClient(addr, true, nil, false)
|
|
_, err := c.LoadModel(context.Background(), &pb.ModelOptions{Model: loadedModel})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(b.loaded).To(Equal(loadedModel))
|
|
return c, b
|
|
}
|
|
|
|
It("rejects every PredictOptions RPC when the identity names another model", func() {
|
|
c, b := newServed("test://identity-mismatch", "a.gguf")
|
|
|
|
for _, err := range callAll(c, &pb.PredictOptions{ModelIdentity: "b.gguf", Prompt: "hi"}) {
|
|
Expect(err).To(HaveOccurred())
|
|
Expect(grpcerrors.IsModelMismatch(err)).To(BeTrue(), "want a mismatch error, got %v", err)
|
|
// The router reacts differently to the two signals, so a mismatch
|
|
// must never be mistaken for a not-loaded.
|
|
Expect(grpcerrors.IsModelNotLoaded(err)).To(BeFalse())
|
|
}
|
|
Expect(b.served).To(Equal(0), "no request may reach the model on a mismatch")
|
|
})
|
|
|
|
It("serves when the identity matches the loaded model", func() {
|
|
c, b := newServed("test://identity-match", "a.gguf")
|
|
|
|
for _, err := range callAll(c, &pb.PredictOptions{ModelIdentity: "a.gguf", Prompt: "hi"}) {
|
|
Expect(err).ToNot(HaveOccurred())
|
|
}
|
|
Expect(b.served).To(Equal(4))
|
|
})
|
|
|
|
// Compatibility, old controller -> new backend. Every existing deployment
|
|
// sends no identity, and tests/e2e-backends/backend_test.go drives real
|
|
// backends with bare PredictOptions at 8+ call sites. Tightening this
|
|
// breaks all of them, so it must fail here first.
|
|
It("serves when the request carries no identity", func() {
|
|
c, b := newServed("test://identity-empty-request", "a.gguf")
|
|
|
|
for _, err := range callAll(c, &pb.PredictOptions{Prompt: "hi"}) {
|
|
Expect(err).ToNot(HaveOccurred())
|
|
}
|
|
Expect(b.served).To(Equal(4))
|
|
})
|
|
|
|
// The backend side of the same rule: a model loaded without an identity
|
|
// (an old controller did the load) cannot judge anything, so it must serve.
|
|
It("serves when the backend has no recorded identity", func() {
|
|
c, b := newServed("test://identity-empty-loaded", "")
|
|
|
|
for _, err := range callAll(c, &pb.PredictOptions{ModelIdentity: "b.gguf", Prompt: "hi"}) {
|
|
Expect(err).ToNot(HaveOccurred())
|
|
}
|
|
Expect(b.served).To(Equal(4))
|
|
})
|
|
})
|