Skip to content
Open
Changes from all 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
18 changes: 18 additions & 0 deletions archipy/helpers/decorators/sqlalchemy_atomic.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
DatabaseTransactionError,
InternalError,
)
import asyncio

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -243,6 +244,19 @@ async def async_wrapper(*args: Any, **kwargs: Any) -> R:
async with session.begin():
result = await func(*args, **kwargs)
return result
except asyncio.CancelledError:
# Real scenarios: request cancelled (client disconnect), timeouts (asyncio/AnyIO),
# TaskGroup/structured concurrency cancellation, or graceful shutdown.
#
# In Python 3.14, CancelledError inherits from BaseException (not Exception),
# so it bypasses `except Exception:` unless handled explicitly.
# We rollback for DB safety and re-raise to preserve cancellation semantics.
await asyncio.shield(session.rollback())
raise
except (SystemExit, KeyboardInterrupt):
# Process is exiting (shutdown / Ctrl+C). Roll back to avoid leaving a transaction open, happens in dev mostly i think.
await asyncio.shield(session.rollback())
raise
except Exception as exception:
await session.rollback()
func_name = getattr(func, "__name__", "unknown")
Expand Down Expand Up @@ -291,6 +305,10 @@ def sync_wrapper(*args: Any, **kwargs: Any) -> R:
else:
with session.begin():
return func(*args, **kwargs)
except (KeyboardInterrupt, SystemExit):
# Ctrl+C / shutdown: rollback for DB safety, then re-raise.
session.rollback()
raise
except Exception as exception:
session.rollback()
func_name = getattr(func, "__name__", "unknown")
Expand Down