You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Write endpoints respond before the database transaction is committed #378
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.
Activity
Commit database transactions before the response is sent
Jayant-kernel commentedon Nov 20, 2026
@Akshats-git
Hi, I noticed there’s already a fix in #379. Would any additional testing be useful?
I’d be happy to help