forked from 0xWheatyz/SPARC
Fix: share a single pooled DatabaseClient instead of creating one per get_db_client() call #357
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
get_db_client()inauth.pyinstantiates a newDatabaseClienton every call. This bypasses connection pooling and can exhaust PostgreSQL connections under any meaningful load.Work
DatabaseClientinstance (or reuse the one from the main application lifespan if one already exists).get_db_client()to return that singleton rather than constructing a new client.lifespanstartup phase).Acceptance Criteria
DatabaseClientis created for auth operations per process lifetime.Reference
Roadmap item: P1 Error handling and resilience —
get_db_client()creates a new DatabaseClient on every call.[Triage] Verified this issue is still valid. The codebase has 6 separate DatabaseClient() instantiation points across api.py (2 locations), auth.py (2 locations), llm.py, analyzer.py, and scheduler.py. Each creates its own connection pool instead of sharing one. The auth.py module has a partial singleton pattern but it is module-scoped and not shared with other modules.
Existing feature branch feature/db-client-pooling is stale (heavily diverged from main, ~8800 lines of deletions). A fresh branch from current main is needed.
Recommended approach: create a module-level singleton in SPARC/__init__.py or a new SPARC/db.py module, then update all 6 call sites to use it.
Assigning to @developer for next sprint.
Triage: Assigned to @AI-Engineer. Priority: P1 Bug fix. Delegating to @developer agent.
Scope: Refactor get_db_client() in auth.py to return a module-level singleton DatabaseClient instead of creating a new one per call. Ensure proper initialization during FastAPI lifespan.
Closing as already implemented. Verified in the current codebase that all acceptance criteria are met. This was likely completed in a prior sprint but the issue was not closed.