Skip to content

Write endpoints respond before the database transaction is committed #378

Open

Description

@Akshats-git

Write endpoints send their response before the database transaction is committed.

What happens

expdb_session and userdb_session commit in the exit code of a yield dependency. Since FastAPI 0.118, that exit code runs after the response is sent. FastAPI is not pinned, so new installs get this behaviour.

So a client can get a 200 before its data is saved. Two things follow:

  • A client that writes and then immediately reads can miss its own write.
  • If the commit fails, the client was already told it worked, and the data is lost.

This affects every write endpoint (tag, untag, status update, studies, delete user), and the upload endpoints we still want to add.

How I checked

FastAPI 0.117.1 commits first, 0.118.0 and newer respond first. With a real uvicorn server and the same dependency code, I did a POST and then a GET right after:

  • default scope: 17/300 and 152/300 stale reads (two runs)
  • scope="function": 0/300

The tests don't catch this, because py_api overrides both session dependencies.

Proposed fix

Use Depends(..., scope="function") for the session and user dependencies (needs FastAPI >= 0.121). That commits before the response is sent. I would also add a test that checks the commit order, and a test that fails if a route uses these dependencies without that scope.

I have a PR ready for this.

Activity

  1. added a commit that references this issue on Nov 15, 2026
    34dd1c0
  2. Jayant-kernel commented on Nov 20, 2026

    @Jayant-kernel

    @Akshats-git
    Hi, I noticed there’s already a fix in #379. Would any additional testing be useful?
    I’d be happy to help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions