Skip to content

Protect against concurrent duplicated webhooks#286

Merged
waiting-for-dev merged 1 commit into
masterfrom
waiting-for-dev/race_webhooks
Apr 18, 2023
Merged

Protect against concurrent duplicated webhooks#286
waiting-for-dev merged 1 commit into
masterfrom
waiting-for-dev/race_webhooks

Conversation

@waiting-for-dev

Copy link
Copy Markdown
Contributor

Summary

By acquiring an update lock on the payment, we can ensure that only one concurrent excecution of the webhook handler will manipulate it. That will avoid duplicated log entries and obscure exceptions.

Closes #188

Checklist

Check out our PR guidelines for more details.

The following are mandatory for all PRs:

The following are not always needed:

  • 📖 I have updated the README to account for my changes.
  • 📑 I have documented new code with YARD.
  • 🛣️ I have opened a PR to update the guides.
  • ✅ I have added automated tests to cover my changes.
  • 📸 I have attached screenshots to demo visual changes.

By acquiring an update lock on the payment, we can ensure that only one
concurrent excecution of the webhook handler will manipulate it. That will avoid
duplicated log entries and obscure exceptions.

Closes #188
it "does nothing if the payment is already voided" do
payment_method = create(:stripe_payment_method)
stripe_payment_intent = Stripe::PaymentIntent.construct_from(id: "pi_123")
stripe_payment_intent = Stripe::PaymentIntent.construct_from(id: "pi_123", cancellation_reason: "duplicate")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious about this change, what's the story behind it?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elia, I think this is from a comment that I left some time ago 😅 #250 (comment)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cancellation reason from the event is used to build the log entry message. We don't use it when the payment is already voided.

Before this PR, we skipped fetching it when the payment was already voided. However, we're now fetching it before acquiring the lock (where the voided check needs to happen) only to hold the lock for the minimum required time.

@rainerdema rainerdema left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@waiting-for-dev waiting-for-dev merged commit 1eb082a into master Apr 18, 2023
@waiting-for-dev waiting-for-dev deleted the waiting-for-dev/race_webhooks branch April 18, 2023 12:57
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.

Be resilient against duplicated webhook events

3 participants