AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
feat(backend): add timeout guard for locked_transaction used for credit transactions (#10528)
Zamil Majdy1c3fa804d4a8
diff --git a/autogpt_platform/backend/backend/data/db.py b/autogpt_platform/backend/backend/data/db.pyindex c3adc39b5a..9ac734fa71 100644--- a/autogpt_platform/backend/backend/data/db.py+++ b/autogpt_platform/backend/backend/data/db.py@@ -1,6 +1,5 @@import loggingimport os-import zlibfrom contextlib import asynccontextmanagerfrom urllib.parse import parse_qsl, urlencode, urlparse, urlunparsefrom uuid import uuid4@@ -88,35 +87,50 @@ TRANSACTION_TIMEOUT = 15000 # 15 seconds - Increased from 5s to prevent timeout@asynccontextmanager-async def transaction(timeout: int | None = None):+async def transaction(timeout: int = TRANSACTION_TIMEOUT):"""Create a database transaction with optional timeout.Args:timeout: Transaction timeout in milliseconds. If None, uses TRANSACTION_TIMEOUT (15s)."""- if timeout is None:- timeout = TRANSACTION_TIMEOUT-async with prisma.tx(timeout=timeout) as tx:yield tx@asynccontextmanager-async def locked_transaction(key: str, timeout: int | None = None):+async def locked_transaction(key: str, timeout: int = TRANSACTION_TIMEOUT):"""- Create a database transaction with advisory lock.+ Create a transaction and take a per-key advisory *transaction* lock.++ - Uses a 64-bit lock id via hashtextextended(key, 0) to avoid 32-bit collisions.+ - Bound by lock_timeout and statement_timeout so it won't block indefinitely.+ - Lock is held for the duration of the transaction and auto-released on commit/rollback.Args:- key: Lock key for advisory lock- timeout: Transaction timeout in milliseconds. If None, uses TRANSACTION_TIMEOUT (15s).+ key: String lock key (e.g., "usr_trx_<uuid>").+ timeout: Transaction/lock/statement timeout in milliseconds."""- if timeout is None:- timeout = TRANSACTION_TIMEOUT-- lock_key = zlib.crc32(key.encode("utf-8"))async with transaction(timeout=timeout) as tx:- await tx.execute_raw("SELECT pg_advisory_xact_lock($1)", lock_key)+ # Ensure we don't wait longer than desired+ # Note: SET LOCAL doesn't support parameterized queries, must use string interpolation+ await tx.execute_raw(f"SET LOCAL statement_timeout = '{int(timeout)}ms'") # type: ignore[arg-type]+ await tx.execute_raw(f"SET LOCAL lock_timeout = '{int(timeout)}ms'") # type: ignore[arg-type]++ # Block until acquired or lock_timeout hits+ try:+ await tx.execute_raw(+ "SELECT pg_advisory_xact_lock(hashtextextended($1, 0))",+ key,+ )+ except Exception as e:+ # Normalize PG's lock timeout error to TimeoutError for callers+ if "lock timeout" in str(e).lower():+ raise TimeoutError(+ f"Could not acquire lock for key={key!r} within {timeout}ms"+ ) from e+ raise+yield txdiff --git a/autogpt_platform/backend/backend/util/retry.py b/autogpt_platform/backend/backend/util/retry.pyindex 1154c46546..79f3d715cf 100644--- a/autogpt_platform/backend/backend/util/retry.py+++ b/autogpt_platform/backend/backend/util/retry.py@@ -152,7 +152,7 @@ def conn_retry(# Preconfigured retry decorator for general functions-func_retry = create_retry_decorator(max_attempts=5, reraise=False)+func_retry = create_retry_decorator(max_attempts=5)def continuous_retry(*, retry_delay: float = 1.0):