This repository was archived by the owner on Jan 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Fix race condition resulting in duplicate payments #12
Milestone
Comments
Verified a v5 task on dev. Payments are getting created only once. Unable to verify a challenge payment in dev due to this issue: To verify payments in prod, we will access to a project with an active billing account. |
This issue is observed in production again. |
This was referenced Mar 3, 2022
This was referenced Mar 4, 2022
This was referenced Mar 4, 2022
Merged
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Root cause
The issue is in Legacy Payment Processor
What happens in Legacy Payment Processor
Read the “challenge.notification.update” events.
Check if the challenge is a V5 Task and is completed.
Query Database to see if a payment record exists for the challenge
If no, create a payment
If yes, do nothing.
Problem
Sometimes 2 “challenge.notification.events” are generated for the challenge in quick succession.
Both execute the Steps 2 and Step 3 execute in parallel, leading the code to find no existing payment causing the same challenge being processed twice.
Solution
Wrap that read/write in a transaction. That way if one process reads, sees the payment has not been made, then writes it would be guaranteed to succeed. If another process comes in and tries to start the same transaction it would block until the first finished and see the payment was already made.
The create payment logic is already in a transaction. We need to bring in the select statement in the same transaction too.
legacy-payment-processor/processorService.js at develop · topcoder-platform/legacy-payment-processor
Additionally, we may consider adding a unique constraint in db for user_id, challenge_id, and payment_type_id condition.
Note, if the legacy payment processor isn’t issuing direct queries to the DB and is instead going through an API, we could add an API specifically to issue a payment and it would use a transaction like I described above.
The text was updated successfully, but these errors were encountered: