Skip to content

fix: add threading lock to LRU cache to prevent data corruption under concurrent load (#562)#583

Open
ionfwsrijan wants to merge 9 commits into
param20h:devfrom
ionfwsrijan:fix/issue-562
Open

fix: add threading lock to LRU cache to prevent data corruption under concurrent load (#562)#583
ionfwsrijan wants to merge 9 commits into
param20h:devfrom
ionfwsrijan:fix/issue-562

Conversation

@ionfwsrijan

Copy link
Copy Markdown
Contributor

Description

The in-memory LRU cache (_lru_get/_lru_set) uses bare dict and list with zero synchronization. Four distinct race conditions exist:

  1. _lru_set_lru_order.remove(key): Two threads both find key in _lru_store is True. Thread A calls remove(key) successfully. Thread B calls remove(key) on the already-removed key → ValueError: list.remove(x): x not in list.
  2. Eviction race: Two threads both find len(_lru_store) >= LRU_MAX_SIZE. Both pop from _lru_order and delete from _lru_store — eviction is corrupted.
  3. Check-then-set non-atomicity: Two threads both check if key in _lru_store, both find it absent, both append to _lru_order — duplicate entries.
  4. Partial-write read in _lru_get: While one thread is mid-json.dumps(vector), another reads partially-written JSON → json.JSONDecodeError.

Changes

backend/app/cache.py

  • Added import threading
  • Added module-level _lru_lock = threading.Lock()
  • Wrapped _lru_get, _lru_set, and _lru_delete with with _lru_lock: to make all LRU cache operations atomic and thread-safe.

Impact

  • Eliminates intermittent ValueError and json.JSONDecodeError under concurrent load
  • Prevents stale/partially-written cache entries that could cause semantically incorrect RAG retrieval
  • Affects both document ingestion (embed_texts) and user queries (embed_query)

Closes #562

@ionfwsrijan ionfwsrijan requested a review from param20h as a code owner June 13, 2026 10:28
@ionfwsrijan

Copy link
Copy Markdown
Contributor Author

@param20h Please review this

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.

[BUG] Thread-Unsafe LRU Embedding Cache Causes Data Corruption Under Load

1 participant