Skip to content

fix(embedding): prevent ants worker panic and mutex deadlock on empty/mismatched results - #2520

Merged
lyingbug merged 1 commit into
Tencent:mainfrom
qfzc:fix/embedding-panic-deadlock
Aug 4, 2026
Merged

fix(embedding): prevent ants worker panic and mutex deadlock on empty/mismatched results#2520
lyingbug merged 1 commit into
Tencent:mainfrom
qfzc:fix/embedding-panic-deadlock

Conversation

@qfzc

@qfzc qfzc commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Description

When the embedding provider returns an empty slice of vectors (e.g., length 0) due to API mismatch or empty input, BatchEmbed returns a slice of length 0.
Accessing embedding[i] directly in BatchEmbedWithPool results 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 the ants pool 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

  • Added validation for len(embedding) != len(texts) in BatchEmbedWithPool of internal/models/embedding/batch.go.
  • If the length is mismatched or empty, it sets the first error, safely releases the lock (mu.Unlock()), and returns instead of panicking.

…/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.
@lyingbug
lyingbug merged commit 2b31d36 into Tencent:main Aug 4, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants