Skip to content

Raise when the server answers COMMIT with ROLLBACK - #1411

Open
MadhurBagdare wants to merge 1 commit into
MagicStack:masterfrom
MadhurBagdare:raise-on-commit-rollback
Open

MadhurBagdare wants to merge 1 commit into
MagicStack:masterfrom
MadhurBagdare:raise-on-commit-rollback

Conversation

@MadhurBagdare

Copy link
Copy Markdown

When a statement fails inside a transaction and the error is caught without rolling back to a savepoint, PostgreSQL completes the later COMMIT with the command tag ROLLBACK and discards the transaction. Transaction.commit() ignored that tag, so both await tr.commit() and async with con.transaction(): returned normally and the transaction was marked committed. A later rollback() then reported that the transaction was already committed, although the changes were gone.

__commit() now checks the command tag of a top-level COMMIT. A ROLLBACK tag marks the transaction rolled back and raises InterfaceError. Savepoint release is unchanged, so an error caught inside a nested transaction still lets the outer transaction commit.

Code that catches an error inside a transaction and expects the block to exit normally now gets that error on the way out. Those writes were already being discarded. python -m unittest tests.test_transaction tests.test_timeout tests.test_prepare passed against PostgreSQL 17.11.

Fixes #1400.

When a statement fails inside a transaction and the error is caught
without rolling back to a savepoint, PostgreSQL completes the later
COMMIT with the command tag ROLLBACK. Transaction.commit() ignored
that tag, returned normally, and recorded the transaction as committed.

Check the command tag of a top-level COMMIT. A ROLLBACK tag marks the
transaction rolled back and raises InterfaceError. Savepoint release
is unchanged.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Transaction.commit() returns normally when PostgreSQL answers COMMIT with ROLLBACK

1 participant