Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

PostgreSQL: Transaction rollback errors with "another operation is in progress" #340

@rafalp

Description

@rafalp

I've noticed that if one of my tests fails, every next test will fail with error from async with database claiming that connection is still on.

Upon closer look I've found out that first tests failure triggers transaction's rollback which sends rollback to the database.

However asyncpg.exceptions._base.InterfaceError: cannot perform operation: another operation is in progress will be raised in response to this, resulting in invalid state:

is_connected = True
_transaction_stack = []

This state can't be fixed other by monkeypatching connection object, as trying to call disconnect() will try to pop item from empty _transaction_stack, causing other exception.

Min repro:

database = Database(url="...", force_rollback=True)

async with database:
    await raise_exception()

async with database:  # this will crash because previous database connection was not cleaned up completely.
   database.exec("...")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions