Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deadlock Detected in bulk update payments. #433

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

hemant10yadav
Copy link
Contributor

@hemant10yadav hemant10yadav commented Nov 14, 2024

CCCT-549

Sentry link

  • The deadlock error indicates that two separate transactions were trying to access the same data in conflicting orders, even though the code is using a single transaction.atomic() block.
  • This suggests there are concurrency issues beyond just the single transaction, likely due to the fact that this is a Celery task.
  • The transaction.atomic() block only ensures atomicity within a single Celery task, but does not prevent deadlocks between multiple concurrent tasks.

To resolve this, I have used cache lock so that only one bulk update payment is allowed at a time.

@calellowitz
Copy link
Collaborator

How does this address the issue? If wrapping the whole thing in a transaction does not prevent locks, why would a second nested transaction?

@hemant10yadav hemant10yadav marked this pull request as draft November 26, 2024 06:43
@hemant10yadav hemant10yadav marked this pull request as ready for review November 26, 2024 10:19
Copy link
Collaborator

@calellowitz calellowitz left a comment

Choose a reason for hiding this comment

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

Pending the answer to my question on lock timeout, looks good

payment_ids.append(payment.pk)
update_work_payment_date(access)
lock_key = f"bulk_update_payments_opportunity_{opportunity.id}"
with cache.lock(lock_key, timeout=300):
Copy link
Collaborator

Choose a reason for hiding this comment

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

How confident are you this timeout is long enough? I would have guessed some of the really big ones, might exceed this, or at least come close.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I recently faced this. I think we can aim for around 10 minutes that should be sufficient, I guess.

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.

4 participants