Skip to content

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.

fix(backend/credit): prevent double-application of transactions due to race condition (#10672)

Zamil Majdy312cb0227f8b
diff --git a/autogpt_platform/backend/backend/data/credit.py b/autogpt_platform/backend/backend/data/credit.py
index 5bddbd42ad..d207ec523d 100644
--- a/autogpt_platform/backend/backend/data/credit.py
+++ b/autogpt_platform/backend/backend/data/credit.py
@@ -286,11 +286,17 @@ class UserCreditBase(ABC):
transaction = await CreditTransaction.prisma().find_first_or_raise(
where={"transactionKey": transaction_key, "userId": user_id}
)
-
if transaction.isActive:
return
async with db.locked_transaction(f"usr_trx_{user_id}"):
+
+ transaction = await CreditTransaction.prisma().find_first_or_raise(
+ where={"transactionKey": transaction_key, "userId": user_id}
+ )
+ if transaction.isActive:
+ return
+
user_balance, _ = await self._get_credits(user_id)
await CreditTransaction.prisma().update(
where={
diff --git a/autogpt_platform/backend/backend/server/routers/v1.py b/autogpt_platform/backend/backend/server/routers/v1.py
index 9bcbe07aef..15fb6dfcfd 100644
--- a/autogpt_platform/backend/backend/server/routers/v1.py
+++ b/autogpt_platform/backend/backend/server/routers/v1.py
@@ -458,12 +458,16 @@ async def stripe_webhook(request: Request):
event = stripe.Webhook.construct_event(
payload, sig_header, settings.secrets.stripe_webhook_secret
)
- except ValueError:
+ except ValueError as e:
# Invalid payload
- raise HTTPException(status_code=400)
- except stripe.SignatureVerificationError:
+ raise HTTPException(
+ status_code=400, detail=f"Invalid payload: {str(e) or type(e).__name__}"
+ )
+ except stripe.SignatureVerificationError as e:
# Invalid signature
- raise HTTPException(status_code=400)
+ raise HTTPException(
+ status_code=400, detail=f"Invalid signature: {str(e) or type(e).__name__}"
+ )
if (
event["type"] == "checkout.session.completed"