GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix connection leak when transaction commit/rollback raise an exception by masipcat · Pull Request #498 · encode/databases

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

Conversation

@masipcat

@masipcat masipcat commented Jul 18, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

This fixes the following case:

from databases import Database

import asyncio


async def method():
    database = Database("postgresql://...")
    await database.connect()

    try:
        async with database.connection() as connection:
            await connection.execute(
                """
                CREATE TABLE test (
                    id integer PRIMARY KEY INITIALLY DEFERRED
                );
                """
            )
            async with connection.transaction():
                await connection.execute("insert into test (id) values (1)")
                await connection.execute("insert into test (id) values (1)")
                # The exception UniqueViolationError is raised during txn.commit()
    finally:
        # Without the fix, connection._connection_counter is 1
        print("Before disconnect:", connection._connection_counter)
        # This blocks forever waiting for the leaked connection
        await database.disconnect()
        print("After disconnect")


asyncio.run(method())

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
masipcat and others added 2 commits July 18, 2022 13:16
@masipcat
Fix connection leak when transaction commit/rollback raise an exception
0d3c5ae
@masipcat
Merge branch 'master' into 0.4.3-vinissimus-hotfix
8d05804
@aminalaee

aminalaee commented Jul 28, 2022

Copy link
Copy Markdown
Contributor

Hi, thanks for the PR.
Can you maybe include the tests for it?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@masipcat

masipcat commented Jul 29, 2022

Copy link
Copy Markdown
Author

Yeah no problem I'll add a test

aminalaee reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

masipcat and others added 3 commits July 29, 2022 12:12
@masipcat
Add test to check transaction doesn't leak on error
8ff26fd
@masipcat
Merge branch 'master' into 0.4.3-vinissimus-hotfix
e783a65
@masipcat
Removed @mysql_versions
1a07fa4
masipcat
masipcat commented Jul 29, 2022
View reviewed changes
Comment thread tests/test_databases.py
Comment on lines +554 to +570
async with Database(database_url) as database:
with pytest.raises(Exception) as excinfo:
async with database.connection() as connection:
await connection.execute(
"""
CREATE TABLE test (id integer PRIMARY KEY INITIALLY DEFERRED);
"""
)
async with connection.transaction():
await connection.execute("insert into test (id) values (1)")
await connection.execute("insert into test (id) values (1)")

# During transaction.commit() postgres will raise this exception:
# asyncpg.exceptions.UniqueViolationError: duplicate key value violates unique constraint "test_pkey"
# DETAIL: Key (id)=(1) already exists.
assert "unique constraint" in str(excinfo.value)
assert connection._connection_counter == 0

@masipcat masipcat Jul 29, 2022

Copy link
Copy Markdown
Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aminalaee I think this test it's only valid for postgresql. Is there a way to skip for other databases?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@aminalaee aminalaee Jul 29, 2022

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sure,

databases/tests/test_databases.py

Line 482 in 6a52a4a

async def test_transaction_commit_serializable(database_url):

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
aminalaee
aminalaee reviewed Jul 29, 2022
View reviewed changes
Comment thread tests/test_databases.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@masipcat
Merge branch 'master' into 0.4.3-vinissimus-hotfix
93a6d69
@ODD2 ODD2 mentioned this pull request Apr 17, 2023
Unable to reconnect database backend after nested transaction exception. #538
Open

This branch has not been deployed

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

Reviewers

1 more reviewer
@aminalaee aminalaee aminalaee left review comments
Reviewers whose approvals may not affect merge requirements

At least 1 approving review is required to merge this pull request.

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@masipcat @aminalaee
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.