Repository navigation
fix: open a Transaction in the read use cases so a retry can recover - #51
Merged
Merged
Conversation
A retry on the shared request session failed with PendingRollbackError after a lost connection, because the session was still inside the failed transaction.
This was referenced Oct 16, 2026
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.
Follow-up to modern-python/db-retry#68.
Six use cases had
@postgres_retrybut ran their queries directly on the injected request session:authenticate_user,check_token_revoked,fetch_chat,fetch_chats,fetch_messagesandfetch_user. When the session's pooled connection dies, the first attempt fails and leaves the session inside the failed transaction.postgres_retryhas no handle on the session, so the retry reuses it and fails withPendingRollbackError: Can't reconnect until invalid transaction is rolled back. The auth middleware callscheck_token_revokedon the request session before the handler runs, so every authenticated request takes this path.Each of the six now takes
transaction: Transaction, like the write use cases, and runs its queries insideasync with self.transaction:. When an attempt fails,Transaction.__aexit__rolls back and closes the session, and the next attempt checks out a fresh connection.Each one also commits before leaving the block.
__aexit__rolls back any open transaction, and a rollback expires every instance the session loaded, so without the commit the returned user, chat or messages come back detached and expired. My first version skipped the commit and 26 existing tests failed. Because the session usesexpire_on_commit=False, committing keeps the objects loaded. Thereturnsits after the block, so these use cases don't need the comment thatedit_messagehas.authenticate_usercommits before the argon2 check, so a login no longer keeps a transaction open while it hashes.Test
tests/use_cases/test_connection_loss.pyresolves each use case from a request container backed by a real engine. The rolled-back test connection can't be used here because it cannot reconnect. The fixture runs a query on the request session, terminates that backend withpg_terminate_backend, waits until asyncpg sees the connection close, and then the test calls the use case.db-retry is not bumped
modern-python/db-retry#69 makes
postgres_retryretry asyncpg'sInterfaceError: connection is closed. It is still open, and the latest release (0.7.0) doesn't include it. With released db-retry the first attempt is never retried, so the new tests can't pass yet. They are marked strictxfail, withraises=limited to the two errors a missing retry produces here: advanced-alchemy's wrappedIntegrityErrorand SQLAlchemy'sInterfaceError. When db-retry ships #69 and the lock is upgraded, the tests will XPASS and strict mode will fail the run. Delete thepytestmarkat that point.Verified locally
just teston db-retry 0.5.1 (the current lock): 123 passed, 6 xfailed, 100% coverage.PendingRollbackError.ruff check,ruff formatandty checkare clean.AGENTS.mdnow says read-only use cases open aTransactiontoo, and why.