fix(embedding): prevent ants worker panic and mutex deadlock on empty/mismatched results - #2520
Merged
Merged
Conversation
…/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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
When the embedding provider returns an empty slice of vectors (e.g., length 0) due to API mismatch or empty input,
BatchEmbedreturns a slice of length 0.Accessing
embedding[i]directly inBatchEmbedWithPoolresults in an index out of bounds panic:runtime error: index out of range [0] with length 0.Because the panic occurs inside a
mu.Lock()block in theantspool worker goroutine, the mutex is never unlocked. This leaves the semaphore locked permanently, leading to a deadlock of the entire document processing queue (e.g.,wg.Wait()hangs forever).Changes
len(embedding) != len(texts)inBatchEmbedWithPoolofinternal/models/embedding/batch.go.mu.Unlock()), and returns instead of panicking.