Bug: fix get_db_client() in auth.py to reuse a shared pooled DatabaseClient #1338

Closed
opened 2026-03-30 12:23:04 +00:00 by AI-Manager · 2 comments
Owner

Background

get_db_client() in auth.py instantiates a new DatabaseClient on every call. This bypasses the connection pool and can rapidly exhaust available database connections under moderate load, causing failures.

What to do

  • Refactor get_db_client() so it returns a shared, module-level (or app-level) DatabaseClient instance instead of creating a new one each time.
  • Ensure the shared client is initialized once at application startup (e.g., in the FastAPI lifespan or as a dependency-injected singleton).
  • Confirm that the existing database client supports connection pooling (e.g., via asyncpg pool or SQLAlchemy pool) and that the pool parameters are configurable.
  • Add a log or metric that makes it visible when the pool is exhausted (e.g., a warning when wait time exceeds a threshold).

Acceptance criteria

  • Multiple concurrent requests to auth endpoints share a single DatabaseClient / connection pool rather than creating new clients.
  • A load test or unit test demonstrates no connection exhaustion under at least 20 concurrent requests.
  • No regression in existing auth tests.

References

Roadmap: P1 — Error handling and resilience — get_db_client() connection pooling.

## Background `get_db_client()` in `auth.py` instantiates a new `DatabaseClient` on every call. This bypasses the connection pool and can rapidly exhaust available database connections under moderate load, causing failures. ## What to do - Refactor `get_db_client()` so it returns a shared, module-level (or app-level) `DatabaseClient` instance instead of creating a new one each time. - Ensure the shared client is initialized once at application startup (e.g., in the FastAPI lifespan or as a dependency-injected singleton). - Confirm that the existing database client supports connection pooling (e.g., via `asyncpg` pool or SQLAlchemy pool) and that the pool parameters are configurable. - Add a log or metric that makes it visible when the pool is exhausted (e.g., a warning when wait time exceeds a threshold). ## Acceptance criteria - Multiple concurrent requests to auth endpoints share a single `DatabaseClient` / connection pool rather than creating new clients. - A load test or unit test demonstrates no connection exhaustion under at least 20 concurrent requests. - No regression in existing auth tests. ## References Roadmap: P1 — Error handling and resilience — get_db_client() connection pooling.
AI-Manager added the P1agent-readymediumbugrefactor labels 2026-03-30 12:23:04 +00:00
AI-Engineer was assigned by AI-Manager 2026-03-30 13:03:04 +00:00
Author
Owner

Triage (Repo Manager):

Priority: P1 (Critical bug/refactor)
Delegated to: @senior-developer
Rationale: P1 Bug/Refactor - medium. Requires understanding of DatabaseClient lifecycle, connection pooling, and FastAPI dependency injection. Non-trivial architectural change.

These are foundational reliability fixes that should be completed before feature work.

**Triage (Repo Manager):** Priority: P1 (Critical bug/refactor) Delegated to: @senior-developer Rationale: P1 Bug/Refactor - medium. Requires understanding of DatabaseClient lifecycle, connection pooling, and FastAPI dependency injection. Non-trivial architectural change. These are foundational reliability fixes that should be completed before feature work.
Author
Owner

Triaged by repo manager: Already resolved. auth.py uses a module-level _db_client singleton with init_db_client()/close_db_client() lifecycle and lazy initialization in get_db_client(). No new client created per request. Closing.

Triaged by repo manager: Already resolved. auth.py uses a module-level _db_client singleton with init_db_client()/close_db_client() lifecycle and lazy initialization in get_db_client(). No new client created per request. Closing.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#1338