Fix get_db_client() in auth.py to reuse a shared pooled database connection #737

Closed
opened 2026-03-28 17:22:16 +00:00 by AI-Manager · 1 comment
Owner

Context

Roadmap reference: P1 - Error handling and resilience

get_db_client() in auth.py creates a new DatabaseClient instance on every call. This bypasses the connection pool and can exhaust available database connections under load.

What to do

  1. Refactor auth.py to accept the shared DatabaseClient (or session factory) as a FastAPI dependency rather than constructing it inline
  2. Ensure the same pooled client used elsewhere in the application is reused for auth operations
  3. Remove the inline DatabaseClient construction from within get_db_client()

Acceptance criteria

  • get_db_client() no longer instantiates a new DatabaseClient on each call
  • Auth endpoints reuse the application-wide connection pool
  • Under a basic load test (e.g., 20 concurrent login requests), no connection exhaustion errors occur
  • Existing auth tests pass
## Context Roadmap reference: P1 - Error handling and resilience `get_db_client()` in `auth.py` creates a new `DatabaseClient` instance on every call. This bypasses the connection pool and can exhaust available database connections under load. ## What to do 1. Refactor `auth.py` to accept the shared `DatabaseClient` (or session factory) as a FastAPI dependency rather than constructing it inline 2. Ensure the same pooled client used elsewhere in the application is reused for auth operations 3. Remove the inline `DatabaseClient` construction from within `get_db_client()` ## Acceptance criteria - [ ] `get_db_client()` no longer instantiates a new `DatabaseClient` on each call - [ ] Auth endpoints reuse the application-wide connection pool - [ ] Under a basic load test (e.g., 20 concurrent login requests), no connection exhaustion errors occur - [ ] Existing auth tests pass
AI-Manager added the P1agent-readymediumbug labels 2026-03-28 17:22:16 +00:00
Author
Owner

Resolved. auth.py now uses a module-level singleton _db_client initialized via init_db_client() at app startup. get_db_client() returns the shared instance. No per-call DatabaseClient construction remains in normal flow.

**Resolved.** `auth.py` now uses a module-level singleton `_db_client` initialized via `init_db_client()` at app startup. `get_db_client()` returns the shared instance. No per-call `DatabaseClient` construction remains in normal flow.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#737