Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions asyncpg/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ async def start(self):
await self._connection.execute(query)
except BaseException:
self._state = TransactionState.FAILED
if con._top_xact is self:
con._top_xact = None
raise
else:
self._state = TransactionState.STARTED
Expand Down
25 changes: 25 additions & 0 deletions tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0


import asyncio

import asyncpg

from asyncpg import _testbase as tb
Expand Down Expand Up @@ -180,6 +182,29 @@ async def test_transaction_within_manual_transaction(self):
self.assertIsNone(self.con._top_xact)
self.assertFalse(self.con.is_in_transaction())

async def test_transaction_failed_begin(self):
self.assertIsNone(self.con._top_xact)

# Make BEGIN fail by running it while another
# operation is in progress on the connection.
busy = asyncio.ensure_future(self.con.execute('SELECT pg_sleep(0.5)'))
await asyncio.sleep(0.1)
with self.assertRaisesRegex(asyncpg.InterfaceError,
'another operation is in progress'):
await self.con.transaction().start()
await busy

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we do something like this to avoid relying on flaky loop timing?

Suggested change
# Make BEGIN fail by running it while another
# operation is in progress on the connection.
busy = asyncio.ensure_future(self.con.execute('SELECT pg_sleep(0.5)'))
await asyncio.sleep(0.1)
with self.assertRaisesRegex(asyncpg.InterfaceError,
'another operation is in progress'):
await self.con.transaction().start()
await busy
tr = self.con.transaction()
error = asyncpg.PostgresConnectionError(
'Timed-out waiting to acquire database connection.'
)
with mock.patch.object(
type(self.con), 'execute', side_effect=error
) as execute:
with self.assertRaises(asyncpg.PostgresConnectionError) as caught:
await tr.start()
self.assertIs(caught.exception, error)
execute.assert_awaited_once_with('BEGIN;')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for catching that, Elvis.

The sleep was a bad way to force BEGIN to fail, and a slow loop could miss the window.

I switched the test to patch execute with a PostgresConnectionError, the way you suggested.

It still checks that _top_xact is cleared and that the next transaction() is a real top-level one, because those are the checks that fail without the fix.

The mock only wraps start(), so that follow-up runs on the live connection.

Happy to trim it if you'd rather the test stop where your suggestion did.


self.assertIsNone(self.con._top_xact)
self.assertFalse(self.con.is_in_transaction())

# The next transaction must be a real top-level one.
async with self.con.transaction():
self.assertTrue(self.con.is_in_transaction())
await self.con.execute('SELECT 1')

self.assertIsNone(self.con._top_xact)
self.assertFalse(self.con.is_in_transaction())

async def test_isolation_level(self):
await self.con.reset()
default_isolation = await self.con.fetchval(
Expand Down