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

Fix atomic transaction not routing to the the correct DB in DatabaseBackend.on_chord_part_return transaction.atomic #427

Merged
merged 6 commits into from May 4, 2024
2 changes: 1 addition & 1 deletion django_celery_results/backends/database.py
Expand Up @@ -246,7 +246,7 @@ def on_chord_part_return(self, request, state, result, **kwargs):
if not gid or not tid:
return
call_callback = False
with transaction.atomic():
with transaction.atomic(using=ChordCounter.objects.db):
Copy link
Contributor

Choose a reason for hiding this comment

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

BaseManager.objects.db returns the django.db.router.db_for_read() which in your situation works since it returns the same db alias as the db_for_write().

https://github.com/django/django/blob/2be37b253341cfd1f1363c533e6f896230f047a7/django/db/models/manager.py#L143

Consider using django.db.router.db_for_write(ChordCounter) instead as we will be updating the ChordCounter.count value within the transaction.

# We need to know if `count` hits 0.
# wrap the update in a transaction
# with a `select_for_update` lock to prevent race conditions.
Expand Down