-
Notifications
You must be signed in to change notification settings - Fork 91
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
Fetch the task state only once at the beginning and store it in local variable #92
Conversation
…re it in local variable
@OmarWKH are you able to review this one? I think you have the most context. |
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.
I suggest also storing other values that we use from self.result, not just self.result.state. They may have the same problem as well.
Looking at celery's source code, anything that uses _get_task_meta may be changed between calls / retrievals if the state is not one of READY_STATES.
https://docs.celeryproject.org/en/stable/_modules/celery/result.html
@OmarWKH good point. It seems like the only other field that could be subject to a race condition is |
…hange between the time when we fetch "state" and the time we fetch "info"
@mobiware that seems to be the way to go. This means the client will not know about the actual current state until the next update. Which is fine. There are 2 places that can still be out of sync:
|
@OmarWKH I don't think those 2 calls are problematic, because they only happen in the
So in both cases we know This is far from ideal since it leans on the knowledge of some Celery internals specifics, but this part of Celery source code (the |
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.
Thanks for working through this and for the clear discussion around the pros/cons and edge cases. Sounds like you reached the best of a few options - all of which had some downside. Hope celery doesn't mess with their private API! 🙏 |
This addresses the following issue:
#80
Looking at Celery code, it turns out that in some cases,
self.result.state
is being fetched from Celery's backend every time we access it, which means that it can change while we are comparing it to various state constants, as suspected in #80 (comment)In fact this PR implements just what the above comment ends up suggesting as a potential fix.