-
Notifications
You must be signed in to change notification settings - Fork 262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix connection leak when transaction commit/rollback raise an exception #498
base: master
Are you sure you want to change the base?
Conversation
Hi, thanks for the PR. |
Yeah no problem I'll add a test |
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 |
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?
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): |
This fixes the following case: