Skip to content

feat(payments): add distributed advisory lock for concurrent checkout session prevention#856

Merged
temma02 merged 1 commit into
StellerCraft:mainfrom
Nexha-dev:feat/payment-checkout-distributed-lock
Jun 27, 2026
Merged

feat(payments): add distributed advisory lock for concurrent checkout session prevention#856
temma02 merged 1 commit into
StellerCraft:mainfrom
Nexha-dev:feat/payment-checkout-distributed-lock

Conversation

@Nexha-dev

Copy link
Copy Markdown
Contributor

Closes #763

Summary

Prevents two concurrent checkout requests from the same user from creating duplicate Stripe sessions by guarding createCheckoutSession() with a Supabase advisory lock.

Changes

  • Add acquireAdvisoryLock(key, timeoutMs) / releaseAdvisoryLock(key) in apps/backend/src/lib/supabase/supabase-lock.ts (backed by pg_try_advisory_lock with polling until the 10s timeout).
  • Wrap createCheckoutSession() in payment.service.ts with the lock (key payment_checkout_{userId}); the lock is released on both success and error paths via finally.
  • Throw CheckoutLockError when the lock cannot be acquired; the checkout route returns 409 Conflict with Retry-After: 10.
  • Tests for lock acquisition, release on success, release on error, lock-not-acquired rejection, and concurrent-request rejection.

Notes

  • Lock key: payment_checkout_{userId}
  • Lock timeout: 10s (released after timeout to avoid deadlock)

🤖 Generated with Claude Code

@drips-wave

drips-wave Bot commented Jun 26, 2026

Copy link
Copy Markdown

@Nexha-dev Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@temma02 temma02 merged commit e710663 into StellerCraft:main Jun 27, 2026
0 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Idempotent Payment Processing with Distributed Lock for Concurrent Checkout Prevention

2 participants