AOSSIE-Org / AOSSIE-Org/Devr.AI
BUG:Race Condition Risk in EmbeddingService Model Initialization
- 主要言語
- Python
- スター
- 102
- フォーク
- 137
- PR マージ指標
- 30日以内にマージされた PR はありません
説明
### Is there an existing issue for this?
- [x] I have searched the existing issues
### What happened?
**Issue Overview**
The model property in EmbeddingService lazy-loads the SentenceTransformer model without any thread-safety.
This can cause multiple concurrent requests to load the model at the same time, leading to memory waste, race conditions, or crashes in a multi-threaded or async environment.
**Steps to Reproduce**
1. Start the backend service.
2. Trigger multiple concurrent calls to any method that accesses EmbeddingService.model (e.g., get_embedding or get_embeddings).
3. Observe that the model is loaded multiple times concurrently (check logs or GPU memory usage).
4. Optionally, run stress tests with multiple async profile summaries to see potential blocking or crashes.
**Expected Behavior**
The model should be loaded only once, regardless of how many concurrent requests access it.
No race conditions or duplicate memory usage should occur.
**Actual Behavior**
Multiple threads or async tasks can instantiate the model multiple times concurrently.
This may lead to high memory usage, GPU resource exhaustion, or task failures.
**Suggested Improvements**
Use a thread-safe lock when lazy-loading the model:
import threading
class EmbeddingService:
_model_lock = threading.Lock()
@property
def model(self) -> SentenceTransformer:
if self._model is None:
with self._model_lock:
if self._model is None:
self._model = SentenceTransformer(self.model_name, device=self.device)
return self._model
This ensures only one instance of the model is created, preventing race conditions and resource duplication.
### Record
- [x] I agree to follow this project's Code of Conduct
- [x] I want to work on this issue
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
評価
この issue はまだ評価されていません。