fix(billing): harden concurrency safety and payment failure handling#491
Merged
fix(billing): harden concurrency safety and payment failure handling#491
Conversation
- Add payment_status field (ok/past_due/blocked) to organization_billing - Replace read-modify-write with atomic Postgres RPCs (increment_project_usage, try_claim_monthly_reset) - Add Redis distributed lock to PushMetrics to prevent concurrent Stripe double-reporting - Wrap SaveMetrics in DB transaction with SELECT FOR UPDATE + consistent lock ordering - Differentiate metered vs plan invoice failures in Stripe webhook - Force excess=true for all projects of payment-blocked orgs - Skip processing for orgs with failed monthly reset to prevent stale-counter billing - Use constant-time comparison for MetricsAuth token check - Validate downgrade_to metadata against plan allowlist - Sync period_end/subscription_id on scheduled cancellations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why we need this PR?
The billing system had several concurrency vulnerabilities and incomplete payment failure handling:
Describe your solution
Atomic DB operations (Supabase)
increment_project_usagePostgres RPC: atomicINSERT ... ON CONFLICT DO UPDATEreplaces read-then-write pattern for usage counterstry_claim_monthly_resetPostgres RPC: atomic checkpoint claim ensures only one concurrent caller performs the resetupdateStorageUsageconverted to use.upsert()withonConflictDistributed lock (Go API)
SetNXlock on PushMetrics (5min TTL) prevents concurrent Stripe double-reportingSaveMetricswrapped in DB transaction withSELECT FOR UPDATE+ sorted lock ordering to prevent deadlocksPayment status tracking
payment_statuscolumn onorganization_billing:ok|past_due|blockedblocked(hard-blocks all writes viaexcess=trueon all metric tags)past_due+ schedule downgrade to freeblocked, plan clearspast_due)Safety improvements
downgrade_tometadata validated against allowlist["free", "pro", "team"]resetFailedOrgs— all subsequent steps skip those orgssubtle.ConstantTimeComparefor MetricsAuth token checkauthenticated=falseImplementation Tasks
payment_statuscolumn toorganization_billingschemaincrement_project_usagePostgres RPC functiontry_claim_monthly_resetPostgres RPC functionincrementUsagewith RPC callupdateStorageUsagewith upserthasOrgResetThisMonth/markOrgMonthAsResetwith atomictryClaimMonthlyResetImpact Areas
Checklist
devbranch.