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.

Warn for open transactions when a connection is garbage collected #554

@zevisert

Description

@zevisert

Originally posted by @zevisert in #546 (comment)

Scenarios where a transaction instance is deleted without calling .commit() or .rollback() (perhaps because a database or connection is being garbage collected) are possible if users are using the documented low-level transaction management API, and they didn't set up their try/except/else logic correctly.

# MVE that hangs on master, 0.7.0, 0.6.2, 0.5.5, 0.4.3, 0.3.2, 0.2.6, 0.1.12 
import databases, asyncio, os

async def mve():
    async with databases.Database(os.environ['TEST_DATABASE_URL']) as db:
        transaction = db.transaction()
        await transaction.start()

asyncio.run(mve())

I consider this user-error, but it also makes sense that this hangs on shutdown. From the database driver's perspective, there is an open transaction that has not executed a COMMIT or ROLLBACK statement, better wait for that to happen before shutting down.

The best thing databases could do here is to emit a warning if __del__ (?) is called on a connection with entries that remain in it's ._transaction_stack. Maybe even each of those transactions could have a .rollback() applied to them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clean upRefinement to existing functionality

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions