mirror of
https://github.com/catlog22/Claude-Code-Workflow.git
synced 2026-02-11 02:33:51 +08:00
fix(embedder): add lock protection for cache read operations
Protect fast path cache read in get_embedder() to prevent KeyError during concurrent access and cache clearing operations. Solution-ID: SOL-1735392000001 Issue-ID: ISS-1766921318981-2 Task-ID: T1
This commit is contained in:
@@ -21,7 +21,7 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
# Global embedder cache for singleton pattern
|
||||
_embedder_cache: Dict[str, "Embedder"] = {}
|
||||
_cache_lock = threading.Lock()
|
||||
_cache_lock = threading.RLock()
|
||||
|
||||
|
||||
def get_embedder(profile: str = "code", use_gpu: bool = True) -> "Embedder":
|
||||
@@ -43,15 +43,12 @@ def get_embedder(profile: str = "code", use_gpu: bool = True) -> "Embedder":
|
||||
# Cache key includes GPU preference to support mixed configurations
|
||||
cache_key = f"{profile}:{'gpu' if use_gpu else 'cpu'}"
|
||||
|
||||
# Fast path: check cache without lock
|
||||
if cache_key in _embedder_cache:
|
||||
return _embedder_cache[cache_key]
|
||||
|
||||
# Slow path: acquire lock for initialization
|
||||
# All cache access is protected by _cache_lock to avoid races with
|
||||
# clear_embedder_cache() during concurrent access.
|
||||
with _cache_lock:
|
||||
# Double-check after acquiring lock
|
||||
if cache_key in _embedder_cache:
|
||||
return _embedder_cache[cache_key]
|
||||
embedder = _embedder_cache.get(cache_key)
|
||||
if embedder is not None:
|
||||
return embedder
|
||||
|
||||
# Create new embedder and cache it
|
||||
embedder = Embedder(profile=profile, use_gpu=use_gpu)
|
||||
|
||||
Reference in New Issue
Block a user