From 2b31d3681bd91fb8d3e638bd6dfaa9aefdd55a9b Mon Sep 17 00:00:00 2001 From: ljl <491252042@qq.com> Date: Tue, 4 Aug 2026 15:37:44 +0800 Subject: [PATCH] fix(embedding): prevent ants worker panic and mutex deadlock on empty/mismatched results If the embedding provider returns an empty slice of vectors (e.g. length 0) due to api mismatch or empty input, BatchEmbed will return length 0. Accessing embedding[i] directly in BatchEmbedWithPool results in an out of bounds panic. Because the panic occurs inside a mu.Lock() block, the mutex is never unlocked, leading to a permanent deadlock of the document processing queue. This fix checks the returned embeddings slice length and returns an error instead of panicking, releasing the mutex properly. --- internal/models/embedding/batch.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/internal/models/embedding/batch.go b/internal/models/embedding/batch.go index 506948f0..2959e8a2 100644 --- a/internal/models/embedding/batch.go +++ b/internal/models/embedding/batch.go @@ -2,6 +2,7 @@ package embedding import ( "context" + "fmt" "os" "strconv" "sync" @@ -60,6 +61,14 @@ func (e *batchEmbedder) BatchEmbedWithPool(ctx context.Context, model Embedder, mu.Unlock() return } + if len(embedding) != len(texts) { + mu.Lock() + if firstErr == nil { + firstErr = fmt.Errorf("embedding model returned %d embeddings for %d inputs", len(embedding), len(texts)) + } + mu.Unlock() + return + } mu.Lock() for i, text := range texts { if text == nil {