-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Add max retry check in task rejection to prevent infinite requeue (WIP) #9642
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
base: main
Are you sure you want to change the base?
Add max retry check in task rejection to prevent infinite requeue (WIP) #9642
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds logic to limit the number of task requeues by tracking rejection counts and enforcing a maximum retry limit to prevent infinite loops.
- Added a _rejection_count attribute to the Request class.
- Extended the slots for Request to include _rejection_count.
- Updated the reject() method to increment the rejection count, compare it with max_retries, and mark the task as failed when the limit is exceeded.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
can you please also link the related issues? |
After exceeding the retries, will the task We would like to direct failures (including worker lost and hard timeout) to failure queues on redis that are not listened to, sort of like dead letter. I was also considering that there could be an option in Celery to specify a queue to requeue into when rejecting, rather than requeuing always to the same one. But if this change will mean we can access the task during |
I tested by applying the body of the The rejection count comes back as always 0 i.e. this PR seems to have no effect / not do what it claims to. Nor does unconditionally running the |
Fix: Limit Rejected Task Retries to Prevent Infinite Loops
Problem
Currently, when a task is rejected with
requeue=True
, it will be retried indefinitely without any limit. This can lead to infinite message loops if there's a persistent issue that cannot be resolved, potentially causing system resource exhaustion.Solution
Added rejection tracking and max retries handling to the
Request
class to prevent infinite rejection loops:_rejection_count
to track number of rejections per taskreject()
method to respect task'smax_retries
settingMaxRetriesExceededError
Example
Backward Compatibility
max_retries
is not set on the task, behavior remains unchangedmax_retries
is explicitly setRelated Issues (WIP)
Fixes #XXXX (if there's an existing issue)
Documentation
This change improves task reliability by preventing infinite rejection loops while maintaining backward compatibility. Users can now safely use
Reject(requeue=True)
withmax_retries
to control retry behavior.