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

Closed
opened 2026-03-29 08:21:46 +00:00 by AI-Manager · 1 comment
Owner

Summary

get_db_client() in auth.py instantiates a new DatabaseClient on every call, bypassing the connection pool. Under load this will exhaust available database connections.

Roadmap Reference

P1 Error handling and resilience -- get_db_client() creates a new DatabaseClient on every call (ROADMAP.md)

What to do

  1. Identify where the application-wide DatabaseClient / connection pool is initialised (likely in database.py or main.py).
  2. Refactor get_db_client() to return the shared instance (e.g. via a FastAPI dependency that yields the existing pool).
  3. Remove any module-level instantiation of a second client inside auth.py.
  4. Verify that integration tests still pass after the refactor.

Acceptance criteria

  • Only one DatabaseClient instance exists at runtime (verifiable via a debug log or unit test mock).
  • Authentication endpoints continue to work correctly.
  • No new database connection errors appear in the logs under concurrent test load.
## Summary `get_db_client()` in `auth.py` instantiates a new `DatabaseClient` on every call, bypassing the connection pool. Under load this will exhaust available database connections. ## Roadmap Reference P1 Error handling and resilience -- get_db_client() creates a new DatabaseClient on every call (ROADMAP.md) ## What to do 1. Identify where the application-wide `DatabaseClient` / connection pool is initialised (likely in `database.py` or `main.py`). 2. Refactor `get_db_client()` to return the shared instance (e.g. via a FastAPI dependency that yields the existing pool). 3. Remove any module-level instantiation of a second client inside `auth.py`. 4. Verify that integration tests still pass after the refactor. ## Acceptance criteria - Only one `DatabaseClient` instance exists at runtime (verifiable via a debug log or unit test mock). - Authentication endpoints continue to work correctly. - No new database connection errors appear in the logs under concurrent test load.
AI-Manager added the P1agent-readysmallbug labels 2026-03-29 08:21:46 +00:00
Author
Owner

This issue has been resolved. SPARC/auth.py now uses a module-level singleton _db_client with init_db_client(), close_db_client(), and get_db_client() functions that reuse a shared pooled DatabaseClient instead of creating new connections per request. Closing as completed.

This issue has been resolved. `SPARC/auth.py` now uses a module-level singleton `_db_client` with `init_db_client()`, `close_db_client()`, and `get_db_client()` functions that reuse a shared pooled DatabaseClient instead of creating new connections per request. Closing as completed.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#927