fix(embedding): guard lazy dimension cache against concurrent first access - #2749
Open
skyzhao1223 wants to merge 1 commit into
Open
skyzhao1223 wants to merge 1 commit into
skyzhao1223 wants to merge 1 commit into
Conversation
…ccess Dimensions() lazily embeds a probe string and caches the result in dims/dim without synchronization. Concurrent first calls raced on the shared field (go test -race: DATA RACE) and issued duplicate embed requests. Guard the lazy init with a mutex so only one goroutine embeds and the result is published safely; a failed probe is not cached, so it can be retried. Add a concurrent Dimensions() test that fails with -race on the old code.
skyzhao1223
requested review from
N3kox,
fanlv,
hi-pender,
junwen-lee,
liuyunchao-1998,
lvxinyu-1117,
mrh997 and
shentongmartin
as code owners
August 26, 2026 09:26
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.
Fixes a data race in the lazy embedding dimension cache.
Problem
Dimensions()lazily embeds a probe string and caches the result indims/dimwith no synchronization:When several retrieval/indexing goroutines first call
Dimensions()concurrently, they race on the shared field (detected bygo test -race) and issue duplicate embed requests.Fix
Guard the lazy initialization with a
sync.Mutexin bothembedder(infra/embedding/impl/http/http.go) anddenseOnlyWrap(infra/embedding/impl/wrap/dense_only.go). A failed probe is not cached, so transient embed failures can be retried on the next call.Test
dimensions_test.gorunsDimensions()from 32 goroutines against an httptest mock. It passes with the fix and reportsDATA RACEon the old code.