-
Notifications
You must be signed in to change notification settings - Fork 434
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
Celery Task with start_time Set in the Future Causes Blocking of Other Tasks #843
Comments
I found that After implementing the corresponding method, I will call it within |
did you modify it in celery? |
No, I didn’t modify Celery’s code. After further investigation, I found that the issue could be fully resolved within django-celery-beat, so I implemented the fix at that level without modifying Celery. |
Ok make sense |
Description:
I encountered an issue when setting a specific
start_time
for a scheduled task indjango-celery-beat
. If thestart_time
is set to a time in the future, Celery will treat this time as part of theevent_t
tuple, specifically theevent_t.time
field. Celery continues to check whether the task has reached its execution condition.However, if the
start_time
is greater than the current time and the time for the nextcrontab
run has not yet been reached, the task will continue to be added to the heap. As a result, thetick
function continuously processes this task in the loop, causing it to block the execution of other tasks.This kind of exception is unlikely to occur with Celery itself, but when used in conjunction with django-celery-beat, it is easy to reproduce the issue due to the user-defined start_time setting.
Code with Explanation:
Steps to Reproduce:
start_time
in the future for a periodic task.crontab
schedule.start_time
and the nextcrontab
execution time has not yet arrived, the task continues to be added to the heap.event.time
generated by thecrontab
task is the smallest, it will remain at the top of the heap, blocking the execution of other tasks (such as the 10-second interval task).Expected Behavior:
The task should not appear at the top of the heap repeatedly before its execution time, regardless of the schedule. This would prevent unnecessary checks and blocking of other tasks that should run in parallel.
Possible Solution:
The issue can be addressed by modifying the
is_due
method indjango_celery_beat/schedulers.py
. The method should calculate the next valid scheduled time based on thestart_time
and task schedulecrontab
. Tasks should only be checked for execution when this time is reached.link
Since I don't know how to set up a unit testing environment for django-celery-beat, I used manual simulation testing. However, I'm unsure if my code modification is correct and whether it can contribute to the community.
Alternatively, the
tick
method in Celery could be updated to handle tasks with a futureevent_t.time
, preventing them from blocking other tasks until they are ready to execute.Either solution would prevent tasks with a future
event_t.time
from being added to the heap and continuously processed, blocking other tasks.Environment:
celery==5.4.0
Django==5.1.6
django-celery-beat==2.7.0
python==3.12
The text was updated successfully, but these errors were encountered: