Repository navigation
Commit database transactions before the response is sent - #379
Akshats-git wants to merge 1 commit into
Conversation
|
No actionable comments were generated in the recent review. WalkthroughThe FastAPI minimum version is now 0.121. Database session and user dependencies in router endpoints use function scope. New tests check whether commits occur before responses, how commit failures and handler errors are handled, and whether source dependency declarations specify function scope. The development guide documents the scope setting and commit timing. Change: Bug fix · Severity of issue fixed: Medium Merge Risk:
|
Since FastAPI 0.118, the exit code of a yield dependency runs after the response is sent. The session dependencies committed there, so clients could get a success response before the data was saved. Declare the session and user dependencies with scope="function" (needs FastAPI >= 0.121), and add tests for the commit order and the scope. Fixes openml#378
144b858 to
34dd1c0
Compare
Linked issuesChecked at commit 34dd1c0 #378 Write endpoints respond before the database transaction is committed · 4 of 4 met
|
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="src/routers/dependencies.py" line_range="56" />
<code_context>
number_missing_values: Annotated[IntegerRange | None, Body()] = None,
status: Annotated[DatasetStatusFilter, Body()] = DatasetStatusFilter.ACTIVE,
- user: Annotated[User | None, Depends(fetch_user)] = None,
+ user: Annotated[User | None, Depends(fetch_user, scope="function")] = None,
) -> list[dict[str, Any]]:
"""List all datasets that match the filters."""
</code_context>
<issue_to_address>
**API fails to start**
When the app registers a route that depends on `fetch_user_or_raise`, fastAPI rejects the dependency graph because `fetch_user_or_raise` is request-scoped while its `fetch_user` child is function-scoped, so route registration fails and the API does not start.
Make the scopes of `fetch_user_or_raise` and `fetch_user` compatible.
</issue_to_address>
### Comment 2
<location path="src/routers/study.py" line_range="128" />
<code_context>
dataset_id: Identifier,
- expdb: Annotated[AsyncSession, Depends(expdb_session)],
- user: Annotated[User | None, Depends(fetch_user)] = None,
+ expdb: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
+ user: Annotated[User | None, Depends(fetch_user, scope="function")] = None,
) -> list[Feature]:
</code_context>
<issue_to_address>
**Retried study creation duplicates records**
When the transaction commits but the response is not received and the client retries a study creation without an alias, `create_study` commits the first study before the response is sent, so a client that times out and retries a request without an alias creates a second study; callers see duplicate studies.
Accept an idempotency key for study creation and return the original result when the same request is retried.
</issue_to_address>
Description
Fixes #378
Write endpoints sent their response before the database transaction was committed. Since FastAPI 0.118, the exit code of a
yielddependency runs after the response is sent, and our session dependencies commit there.This PR declares the session and user dependencies with
scope="function". Now the commit happens before the response is sent. This needs FastAPI >= 0.121, so I added that lower bound.What changed:
scope="function"on allDependsforexpdb_session,userdb_sessionandfetch_user(one-line edits).fetch_userneeds it too, because FastAPI does not allow a request-scoped dependency to use a function-scoped one.fastapi>=0.121inpyproject.toml.tests/dependencies/session_commit_test.py: the commit happens before the response, a failed commit gives a 500 instead of a 200, errors roll back, and every router uses the function scope (this one fails onmain).I measured the effect with a real uvicorn server and a POST followed by a GET. With the old scope, 17/300 and 152/300 reads were stale. With the new scope, 0/300.
The new tests do not need the database. I could not run the full suite locally (no access to the test database image), so please let CI confirm.
Checklist
Always:
Required for code changes:
If applicable:
/docs)Extra context: