fix(python): commit read-only sessions to avoid spurious rollbacks#408
Merged
fix(python): commit read-only sessions to avoid spurious rollbacks#408
Conversation
Every GET request opened an implicit PostgreSQL transaction that was rolled back when the session closed, generating ~200/s rollbacks visible in transaction count metrics during benchmarks. Wrapping the session yield in try/commit/except/rollback ensures: - Read-only transactions are committed (no-op for data, counts as commit in pg metrics instead of rollback) - Write operations that already committed inside the repository have no active transaction left, so the outer commit is a no-op - Exceptions still trigger an explicit rollback and re-raise Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request fixes an issue where read-only database operations (GET requests) were being rolled back instead of committed, causing spurious rollback metrics during benchmarking (~200/s rollbacks vs ~20/s commits). The fix wraps the session yield in a try/commit/except/rollback pattern so that all successful transactions, including read-only ones, are properly committed.
Changes:
- Modified the
get_sessionmethod inDatabaseManagerto explicitly commit successful transactions and rollback on exceptions - This change affects all database operations routed through FastAPI's dependency injection system
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
GETrequest opened an implicit PostgreSQL transaction that was rolled back when the SQLAlchemy session closed (viaasync with session_factory() as session→session.close()→ implicit rollback)yieldintry/commit/except/rollbackso read-only transactions are committed rather than rolled backHow it works
GET /v1/lampsGET /v1/lamps/{id}POST /v1/lampsPUT /v1/lamps/{id}DELETE /v1/lamps/{id}Note: the
create()missing-commit bug was separately fixed in #407.Test plan
cd src/python && poetry run pytest🤖 Generated with Claude Code