Skip to content

Commit database transactions before the response is sent - #379

Open
Akshats-git wants to merge 1 commit into
openml:mainfrom
Akshats-git:fix/commit-before-response
Open

Akshats-git wants to merge 1 commit into
openml:mainfrom
Akshats-git:fix/commit-before-response

Conversation

@Akshats-git

@Akshats-git Akshats-git commented Nov 15, 2026 •

Copy link
Copy Markdown

Description

Fixes #378

Write endpoints sent their response before the database transaction was committed. Since FastAPI 0.118, the exit code of a yield dependency 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 all Depends for expdb_session, userdb_session and fetch_user (one-line edits). fetch_user needs it too, because FastAPI does not allow a request-scoped dependency to use a function-scoped one.
  • fastapi>=0.121 in pyproject.toml.
  • A short note in the dev docs.
  • New tests in 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 on main).

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:

  • I have performed a self-review of my own pull request to ensure it contains all relevant information, and the proposed changes are minimal but sufficient to accomplish their task.

Required for code changes:

  • Tests pass locally
  • I have commented my code in hard-to-understand areas, and provided or updated docstrings as needed
  • Changes are already covered under existing tests
  • I have added tests that cover the changes (only required if not already under coverage)

If applicable:

  • I have made corresponding changes to the documentation pages (/docs)

Extra context:

  • This PR and the commits have been created autonomously by a bot/agent.

@coderabbitai

coderabbitai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The 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: Minimal · up to 34dd1

This change makes database writes commit before the response is sent, so clients no longer see stale reads right after a write. No concrete defects were found, and CI should confirm the test suite.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "Commit database transactions before the response is sent" matches the main change. The PR sets scope="function" on session and user dependencies so that commits run before the response is s…
Description check Passed The description states the problem (write endpoints responding before the transaction commits), the fix (scope="function" on expdb_session, userdb_session and fetch_user), the FastAPI >= 0.121 lower b…
Linked Issues check Passed Issue #378 requires write endpoints to commit before the response is sent. The proposed fix is Depends(..., scope="function") on the session and user dependencies, FastAPI >= 0.121, and tests for co…
Out of Scope Changes check Passed All changes connect to the objective of #378. Each router change adds scope="function" to a session or user dependency. Read-only endpoints in datasets.py, qualities.py, tasks.py, tasktype.py, flows…
Full details: Title check

Explanation

The title "Commit database transactions before the response is sent" matches the main change. The PR sets scope="function" on session and user dependencies so that commits run before the response is sent, which is the stated goal of the PR and linked issue #378.

Full details: Description check

Explanation

The description states the problem (write endpoints responding before the transaction commits), the fix (scope="function" on expdb_session, userdb_session and fetch_user), the FastAPI >= 0.121 lower bound, the dev-doc note, and the new tests. All of these match the changeset.

Full details: Linked Issues check

Explanation

Issue #378 requires write endpoints to commit before the response is sent. The proposed fix is Depends(..., scope="function") on the session and user dependencies, FastAPI >= 0.121, and tests for commit order and for missing scope.

Evidence at head 34dd1c0:

  • expdb_session, userdb_session, and fetch_user usages in the routers now declare scope="function". The summary lists this for the write endpoints named in #378: tag_setup, untag_setup (setups.py), create_study and attach_to_study (study.py), and delete_user_account (users.py). Dataset status update is covered by the dataset router edits.
  • fetch_user and fetch_user_or_raise in src/routers/dependencies.py declare function scope. This is required because a request-scoped dependency cannot use a function-scoped one.
  • pyproject.toml sets fastapi>=0.121.
  • tests/dependencies/session_commit_test.py covers the requested behavior:
    • test_commit_happens_before_response asserts the event order handler, commit, response sent.
    • test_failing_commit_is_not_reported_as_success asserts a 500 on commit failure.
    • test_error_rolls_back asserts rollback without commit.
    • test_session_dependencies_are_function_scoped walks the AST of src and fails on any Depends of a session or user dependency without a constant scope="function".

The commit-order test uses a local app and fake session, not the production routers. The AST check covers direct name usage only. The tests were not run in this review, so CI results are unknown. The code evidence supports compliance.

Full details: Out of Scope Changes check

Explanation

All changes connect to the objective of #378. Each router change adds scope="function" to a session or user dependency. Read-only endpoints in datasets.py, qualities.py, tasks.py, tasktype.py, flows.py, evaluations.py, estimation_procedure.py, and runs.py receive the same change. This is consistent with the issue's proposed fix, and the AST test requires it for every use. The pyproject.toml bound, the dev-doc note, and the new tests directly support the fix. No unrelated behavior change is shown in the summary. Endpoint logic and response shapes are reported as unchanged.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2fa8f57b-14e5-4ed6-8cb3-4591d26651bf
Commits

Reviewing files that changed from the base of the PR and between 621da17 and 34dd1c0.

Files selected for processing: 15

  • docs/development/code.md
  • pyproject.toml
  • src/routers/datasets.py
  • src/routers/dependencies.py
  • src/routers/estimation_procedure.py
  • src/routers/evaluations.py
  • src/routers/flows.py
  • src/routers/qualities.py
  • src/routers/runs.py
  • src/routers/setups.py
  • src/routers/study.py
  • src/routers/tasks.py
  • src/routers/tasktype.py
  • src/routers/users.py
  • tests/dependencies/session_commit_test.py

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

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
@Akshats-git
Akshats-git force-pushed the fix/commit-before-response branch from 144b858 to 34dd1c0 Compare November 15, 2026 08:49
@sourcery-ai

sourcery-ai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Linked issues

Checked at commit 34dd1c0

#378 Write endpoints respond before the database transaction is committed · 4 of 4 met

# Requirement Status Notes
1 Commit session transactions before the response is sent for write endpoints. ✅ Met The route dependencies for expdb_session and userdb_session, and the authentication dependency that uses a session, are declared with scope="function".
2 Use FastAPI >= 0.121 for function-scoped dependencies. ✅ Met pyproject.toml now specifies fastapi>=0.121.
3 Add a test that checks the commit happens before the response. ✅ Met The new test asserts the event order is handler, commit, then response sent.
4 Add a test that fails if a route uses the session or user dependencies without function scope. ✅ Met The AST test scans Python files under src for Depends calls using expdb_session, userdb_session, or fetch_user without scope="function".

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread src/routers/dependencies.py Resolved
Comment thread src/routers/study.py Resolved

This branch has not been deployed

No deployments
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.

Write endpoints respond before the database transaction is committed

1 participant