forked from 0xWheatyz/SPARC
Fix get_db_client() in auth.py to use singleton pattern properly #326
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.pyfalls back to creating a newDatabaseClienton every call if_db_clientis not initialized, and theinit_db_client()function must be called explicitly at startup. If anything callsget_db_client()beforeinit_db_client()runs, a fresh client is created each time — bypassing the connection pool and potentially exhausting database connections under load.What to do
init_db_client()is always called before any auth endpoint is reachable.lifespanor@app.on_event("startup")) that explicitly callsinit_db_client()and raises clearly if the database is unreachable.get_db_client()return the same object instance.Acceptance criteria
get_db_client()always returns the sameDatabaseClientinstance after startup.DATABASE_URLis invalid.Roadmap ref: P1 — Error handling and resilience (
_jobsdict / db client pool)Triage (AI-Manager): Assigned to @AI-Engineer.
This is a P1 small bug fix — singleton pattern issue in
auth.pyget_db_client(). Straightforward fix: ensureinit_db_client()runs at startup via FastAPI lifespan, and thatget_db_client()always returns the same instance. Add a test verifying identity of returned objects.Priority: P1 — address before other work to prevent connection pool exhaustion.
[Repo Manager] This issue is resolved. auth.py already implements a proper singleton pattern with _db_client module-level variable, init_db_client() for startup initialization, get_db_client() for lazy access, and close_db_client() for shutdown cleanup.