db.py: Race-prone module-level AsyncCosmosClient initialization
- Langage dominant
- TypeScript
- Étoiles
- 0
- Forks
- 0
- Merge moyen
- 16 min
- PR mergées (30 j)
- 1
Description
## Problem
`PluckIt.Processor/agents/db.py:72-82` uses a global `_async_client` with a non-synchronized lazy init. Under FastAPI with concurrent requests, multiple coroutines can read `_async_client is None` simultaneously and each create a new `AsyncCosmosClient`.
```python
def get_async_client() -> AsyncCosmosClient:
global _async_client
if _async_client is None:
_async_client = AsyncCosmosClient(...) # not thread-safe
```
## Impact
Under moderate concurrency, duplicate client instances consume unnecessary connection pool slots and can trigger Cosmos DB throttling.
## Proposed Fix
```python
_async_client_lock = asyncio.Lock()
async def get_async_client() -> AsyncCosmosClient:
global _async_client
if _async_client is None:
async with _async_client_lock:
if _async_client is None:
_async_client = AsyncCosmosClient(...)
return _async_client
```
## Functionality Impact
`get_async_client` becomes async — all callers must `await` it.
Guide de contribution
Ouvrir le guide de contribution
Piste de recherche
Le problème se trouve dans PluckIt.Processor/agents/db.py, aux lignes 72-82. Examinez la fonction get_async_client et ses appelants. Ajoutez un asyncio.Lock pour garantir une initialisation thread-safe et convertissez la fonction en fonction asynchrone. Assurez-vous que tous les appelants utilisent désormais await. Exécutez l’application en situation de concurrence pour vérifier qu’aucun client en double n’est créé.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- python
- Domaine
- backend, databases
- Type d'issue
- Bug
- Difficulté
- 2/5
- Temps estimé
- 1-3 heures
- Activité
- À l'abandon
- Clarté
- Clairement spécifiée
- Accessibilité débutants
- 70/100