fix(webapp): commit before the response, app-wide (#851) #854
Loading…
Reference in a new issue
No description provided.
Delete branch "fix/851-commit-before-response"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Closes #851.
The bug
In FastAPI 0.141.1's
request_response, request-scoped generator dependencies are torn down afterresponse(...)has sent the bytes.get_dbcommits in its teardown, so every route that left the commit toget_dbanswered before it committed.That had two effects:
Before this fix, on main:
The fix
Depends(get_db)becomeDepends(get_db, scope="function"). FastAPI's own function scope tears down after the endpoint and serialisation, before the send./record/streamalready closed its session before looping, and/exportstreams from memory. There are no background tasks or WebSocket routes to worry about.notify_vote_cast, so the bot's tally read can't be stale.Tests (
tests/test_commit_before_response.py, new)get_dbdependency isn't function-scoped. On main it reported 1,022 such dependencies. It asserts it found more than 300, so a broken walker can't pass silently.get_db: a second connection checks at the moment the last byte is sent, for vote PUT, vote DELETE and a failing commit. All three failed on main.test_discord_vote_symmetry: a failed.delaydoesn't cost the vote, on both the web and bot paths, and the commit comes before the enqueue.For future code: every new
Depends(get_db)must sayscope="function", and the structural test enforces it. In-flight branches (lanes 6, 7 and 9) get converted when they merge.🤖 Generated with Claude Code