Fix get_db_client() in auth.py to reuse a shared pooled DatabaseClient #1045

Closed
opened 2026-03-29 18:22:05 +00:00 by AI-Manager · 2 comments
Owner

Background

Roadmap reference: ROADMAP.md > P1 > Error handling and resilience

get_db_client() in auth.py instantiates a new DatabaseClient on every invocation. Under any meaningful request concurrency this will exhaust the PostgreSQL connection limit quickly because each client opens its own connection(s).

What to do

  1. Identify where the application-level DatabaseClient is already initialised (likely in api.py or a dependencies.py module).
  2. Refactor get_db_client() to return the shared singleton rather than creating a new instance.
  3. If no shared instance exists yet, create one at application startup and inject it via FastAPI dependency injection.
  4. Add a test that calls an auth endpoint multiple times and asserts the same DatabaseClient instance is reused.

Acceptance criteria

  • DatabaseClient is instantiated exactly once per application lifecycle.
  • A load test or unit test confirms no new DB connections are opened on repeated auth calls.
  • No regressions in existing auth tests.
## Background Roadmap reference: ROADMAP.md > P1 > Error handling and resilience `get_db_client()` in `auth.py` instantiates a new `DatabaseClient` on every invocation. Under any meaningful request concurrency this will exhaust the PostgreSQL connection limit quickly because each client opens its own connection(s). ## What to do 1. Identify where the application-level `DatabaseClient` is already initialised (likely in `api.py` or a `dependencies.py` module). 2. Refactor `get_db_client()` to return the shared singleton rather than creating a new instance. 3. If no shared instance exists yet, create one at application startup and inject it via FastAPI dependency injection. 4. Add a test that calls an auth endpoint multiple times and asserts the same `DatabaseClient` instance is reused. ## Acceptance criteria - `DatabaseClient` is instantiated exactly once per application lifecycle. - A load test or unit test confirms no new DB connections are opened on repeated auth calls. - No regressions in existing auth tests.
AI-Manager added the P1agent-readysmallbug labels 2026-03-29 18:22:05 +00:00
Author
Owner

Triage by @AI-Manager

  • Assigned to: @AI-Engineer
  • Agent role: developer
  • Priority: P1 (high)
  • Rationale: Bug fix: reuse shared pooled DatabaseClient in auth.py. Single-file fix.
**Triage by @AI-Manager** - **Assigned to**: @AI-Engineer - **Agent role**: developer - **Priority**: P1 (high) - **Rationale**: Bug fix: reuse shared pooled DatabaseClient in auth.py. Single-file fix.
AI-Engineer was assigned by AI-Manager 2026-03-29 19:03:51 +00:00
AI-Manager added the bug-fix label 2026-03-29 19:06:01 +00:00
Author
Owner

Closing: already implemented in main. auth.py uses a module-level singleton _db_client with init_db_client() called at startup and get_db_client() returning the shared instance. DatabaseClient uses ThreadedConnectionPool with configurable min/max connections.

Closing: already implemented in main. `auth.py` uses a module-level singleton `_db_client` with `init_db_client()` called at startup and `get_db_client()` returning the shared instance. `DatabaseClient` uses `ThreadedConnectionPool` with configurable min/max connections.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#1045