Defer Celery send_queued_mail until the request transaction commits - #530
Merged
Merged
Conversation
With CELERY_ENABLED and ATOMIC_REQUESTS, email_queued fired inside the still-open request transaction and queued_mail_handler called send_queued_mail.delay() immediately, so the worker could run before commit and get_queued() saw zero rows. Wrap only the Celery delay() in transaction.on_commit. Credits @javialon26 for the diagnosis. Fixes ui#518
CI installs only [test] extras (no celery). QueuedMailHandlerCeleryTests asserted send_queued_mail exists after reload, which fails the ImportError branch in post_office.tasks. Skip the class unless celery can be imported; keep the on_commit race coverage when it is present.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
With
CELERY_ENABLED=Trueand DjangoATOMIC_REQUESTS=True,email_queuedfires inside the still-open request transaction.queued_mail_handlerwas callingsend_queued_mail.delay()immediately, so a Celery worker could run before commit andget_queued()would see zero rows.This wraps only the Celery
send_queued_mail.delay()indjango.db.transaction.on_commitinsidequeued_mail_handler. The no-Celery / Huey path is unchanged.Credits @javialon26 for reporting the race and proposing
on_commit.Fixes #518
Test plan
QueuedMailHandlerCeleryTests.test_send_queued_mail_delay_waits_for_commitfails (delaycalled insideatomic)django-admin test tests→ 151 tests OK (7 skipped)