Skip to content

fix(api): restore Depends(get_db) — db: Any = None is never injected - #847

Open
libingtong wants to merge 1 commit into
dataelement:mainfrom
libingtong:fix/restore-db-dependency-injection
Open

fix(api): restore Depends(get_db) — db: Any = None is never injected#847
libingtong wants to merge 1 commit into
dataelement:mainfrom
libingtong:fix/restore-db-dependency-injection

Conversation

@libingtong

Copy link
Copy Markdown

f0874c3 ("convert Depends(get_db) to db: Any = None across API endpoints to release connection pool early") changed 275 endpoint signatures but only touched app/api/**; no mechanism was added to supply the session.

FastAPI treats an un-annotated db: Any = None parameter as an optional query parameter, so db is always None at runtime and every endpoint that touches it raises:

AttributeError: 'NoneType' object has no attribute 'execute'
AttributeError: 'NoneType' object has no attribute 'add'

Reproduction

Confirmed on a deployment running current main. Both are public, unauthenticated endpoints:

POST /api/enterprise/check-email-exists   -> 500
POST /api/sso/session                     -> 500

Why it is easy to miss

Endpoints that never declared a db parameter and open their own transaction — e.g. POST /api/auth/login — are unaffected, so basic email/password login keeps working.

POST /api/sso/session breaking is the most damaging case: the login page calls it before /sso/config (frontend/src/pages/Login.tsx), so the entire SSO provider section silently fails to render with no visible error.

This PR

Reverts f0874c3 to restore working behaviour.

If the connection-pool optimisation is still wanted, each converted endpoint needs to actually acquire a session in its body (e.g. async with transaction() as db:) rather than only changing the signature. That is a substantially larger change and would benefit from tests that exercise the converted endpoints — none currently cover them, which is why this shipped.

…= None`

This reverts f0874c3 ("convert Depends(get_db) to db: Any = None across API
endpoints to release connection pool early").

That commit changed 275 endpoint signatures but only touched app/api/**; no
mechanism was added to supply the session. FastAPI treats an un-annotated
`db: Any = None` parameter as an optional *query* parameter, so `db` is always
None at runtime and every endpoint that touches it raises:

    AttributeError: 'NoneType' object has no attribute 'execute'
    AttributeError: 'NoneType' object has no attribute 'add'

Confirmed on a deployment running current main — both are public endpoints:

    POST /api/enterprise/check-email-exists   -> 500
    POST /api/sso/session                     -> 500

POST /api/sso/session breaking is particularly damaging because the login page
calls it before /sso/config (frontend/src/pages/Login.tsx), so the whole SSO
provider section silently fails to render.

Endpoints that never declared a `db` parameter and open their own transaction
(e.g. POST /api/auth/login) are unaffected, which is why basic login still works
and the regression is easy to miss.

Reverting restores the working behaviour. If the connection-pool optimisation is
still wanted, it needs each converted endpoint to actually acquire a session in
its body (e.g. `async with transaction() as db:`) rather than only changing the
signature — that is a much larger change and should land with tests that
exercise the converted endpoints.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant