Skip to content

Commit

Permalink
Changes:
Browse files Browse the repository at this point in the history
- fix deadlock better (only cleanup when it is the main database)
- fix Transaction failing over AsyncDatabaseHelper
  • Loading branch information
devkral committed Sep 10, 2024
1 parent a63140a commit a7c9362
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
8 changes: 2 additions & 6 deletions databasez/core/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,7 @@ async def connect(self) -> bool:
)
# prevent side effects of connect_hook
database._call_hooks = False
# only enter when full_isolation is active
if self._global_connection._full_isolation:
database._global_connection = await self._global_connection.__aenter__()
else:
database._global_connection = self._global_connection
database._global_connection = self._global_connection
self._databases_map[loop] = database
# forward call
return await self._databases_map[loop].connect()
Expand Down Expand Up @@ -408,8 +404,8 @@ async def disconnect(

try:
assert self._global_connection is not None
await self._global_connection.__aexit__()
if self._remove_global_connection:
await self._global_connection.__aexit__()
self._global_connection = None
self._connection = None
finally:
Expand Down
14 changes: 10 additions & 4 deletions databasez/core/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ async def wrapper(*args: typing.Any, **kwargs: typing.Any) -> typing.Any:
@multiloop_protector(False)
async def _start(
self,
connection: Connection,
timeout: typing.Optional[
float
] = None, # stub for type checker, multiloop_protector handles timeout
) -> None:
connection = self.connection
assert connection._loop

async with connection._transaction_lock:
Expand All @@ -145,16 +145,22 @@ async def start(
cleanup_on_error: bool = True,
) -> Transaction:
connection = self.connection
# WARNING: we are maybe in the wrong context and get an AsyncDatabaseHelper, so
# - don't pass down the connection
# - assume this is not a connection_transaction
# count up connection and init multithreading-safe the isolation thread
# benefit 2: setup works with transaction_lock
if connection.connection_transaction is not self:
if getattr(connection, "connection_transaction", None) is not self:
await connection.__aenter__()
# we have a loop now in case of full_isolation
try:
await self._start(connection, timeout=timeout)
await self._start(timeout=timeout)
except BaseException as exc:
# normal start call
if cleanup_on_error and connection.connection_transaction is not self:
if (
cleanup_on_error
and getattr(connection, "connection_transaction", None) is not self
):
await connection.__aexit__()
raise exc
return self
Expand Down

0 comments on commit a7c9362

Please sign in to comment.