From 50324f710ab44e8a07db41cc1440c1684abf9037 Mon Sep 17 00:00:00 2001 From: Zamil Majdy Date: Thu, 16 Apr 2026 18:42:09 +0700 Subject: [PATCH] fix(backend): remove dead code and fix misleading docstring MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove the redundant `if invoice_id:` guard in `handle_subscription_payment_failure` — `invoice_id` is guaranteed non-empty by the early-return guard added above it. Fix `_cleanup_stale_subscriptions` docstring that claimed a metric `stripe_stale_subscription_cleanup_failed` is emitted; only a `logger.exception` call is made, so remove the inaccurate metric claim to avoid misleading on-call engineers. --- .../backend/backend/data/credit.py | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/autogpt_platform/backend/backend/data/credit.py b/autogpt_platform/backend/backend/data/credit.py index 12e2a5cdf0..035f45729d 100644 --- a/autogpt_platform/backend/backend/data/credit.py +++ b/autogpt_platform/backend/backend/data/credit.py @@ -1552,9 +1552,8 @@ async def _cleanup_stale_subscriptions(customer_id: str, new_sub_id: str) -> Non NOTE: until that reconcile job lands, a failure here means the user is silently billed for two simultaneous subscriptions. The error log below is intentionally - `logger.exception` so it surfaces in Sentry with the customer/sub IDs needed to - manually reconcile, and the metric `stripe_stale_subscription_cleanup_failed` - is bumped so on-call can alert on persistent drift. + `logger.exception` (not `logger.warning`) so it surfaces as an error in Sentry + with the customer/sub IDs needed for manual reconciliation. TODO(#stripe-reconcile-job): replace this best-effort cleanup with a periodic reconciliation job that queries Stripe for customers with >1 active sub. """ @@ -1824,16 +1823,16 @@ async def handle_subscription_payment_failure(invoice: dict) -> None: # Balance covered the invoice. Pay the Stripe invoice so Stripe's dunning # system stops retrying it — without this call Stripe would retry automatically # and re-trigger this webhook, causing double-deductions each retry cycle. - if invoice_id: - try: - await run_in_threadpool(stripe.Invoice.pay, invoice_id) - except stripe.StripeError: - logger.warning( - "handle_subscription_payment_failure: balance deducted for user" - " %s but failed to mark invoice %s as paid; Stripe may retry", - user.id, - invoice_id, - ) + # invoice_id is guaranteed non-empty here (early-return guard above). + try: + await run_in_threadpool(stripe.Invoice.pay, invoice_id) + except stripe.StripeError: + logger.warning( + "handle_subscription_payment_failure: balance deducted for user" + " %s but failed to mark invoice %s as paid; Stripe may retry", + user.id, + invoice_id, + ) logger.info( "handle_subscription_payment_failure: deducted %d cents from balance" " for user %s; Stripe invoice %s paid, sub %s intact, tier preserved",