-
Notifications
You must be signed in to change notification settings - Fork 504
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
Version 0.17.8 broke Celery tasks with custom Task class #844
Comments
This is curious because the way we monkeypatch should generally work fine with subclasses, and tenanttask does not override apply_async, and the other methods it does override are not called by us. I think there is some magic going on in Celery that I overlooked. This change was not super mission-critical, I only did it because patching instance methods seemed unnecessary. I'll take a look next week unless there is a second user who has this problem as well. |
I think I just ran in to this as well with Flask... here's my code: in base.py import celery
from config.celery import DefaultCeleryConfig
from tasks.error_handling import RetryError
# https://flask.palletsprojects.com/en/1.1.x/patterns/celery/
# Run Celery task inside Flask context
# pylint: disable=W0223
class TaskWithFlaskContext(celery.Task): # noqa: W0223
autoretry_for = (RetryError,)
retry_backoff = DefaultCeleryConfig.RETRY_BACKOFF_DELAY_SECONDS
retry_backoff_max = DefaultCeleryConfig.RETRY_BACKOFF_MAX_SECONDS
max_retries = DefaultCeleryConfig.MAX_RETRIES
retry_jitter = DefaultCeleryConfig.RETRY_JITTER
def __call__(self, *args, **kwargs):
from config.main import setup_application
app = setup_application()
with app.app_context():
return self.run(*args, **kwargs) in worker.py celery = Celery("worker", task_cls="tasks.base:TaskWithFlaskContext") |
judging by the change mentioned by OP I would assume this code from celery.app.task import Task # type: ignore
Task.apply_async = _wrap_apply_async(Task.apply_async) Is patching a class that we're not using (or that get's replaced). In this documentation https://flask.palletsprojects.com/en/1.1.x/patterns/celery/#configure from celery import Celery
def make_celery(app):
celery = Celery(
app.import_name,
backend=app.config['CELERY_RESULT_BACKEND'],
broker=app.config['CELERY_BROKER_URL']
)
celery.conf.update(app.config)
class ContextTask(celery.Task):
def __call__(self, *args, **kwargs):
with app.app_context():
return self.run(*args, **kwargs)
celery.Task = ContextTask
return celery |
Thanks, this snippet helps |
Seems like there's a fix coming already, but I'll just leave a note here that this also broke our Celery tasks which are triggered via celery.tasks.periodic_task. We don't have any subclassing going on. |
@clintmod (and others) can you verify whether it was actually 0.17.8 that broke your application? I am not able to reproduce issues with custom task classes. I have not tried setting up a Django app with tenant-schemas-celery yet. It would be greatly apprechiated if somebody could give me a reproducible example in form of a repo where all dependencies are pinned. |
@untitaker yes, periodic tasks broken by 0.7.18. Periodic task created with |
Aye, 0.17.7 works ok and with 0.17.8 it starts breaking. We don't use tenant-schemas-celery, so the issue should be reproducible with plain Django + Celery. |
Which celery version is this now? I tried specifically periodic_task now and I am still not able to reproduce. Are there Django-specific bits I have to take into account? I didn't see task subclasses in djcelery. Please again somebody give me a testapp. testcase that passes under celery 4-5 (not tested 3): click
|
It seems that the version 0.17.7 was also broken, but didn't appear in my local dev environment. After I updated our stage and production environments to 0.17.7, our Celery tasks stopped working because our TenantTask was not handling the context switching anymore. Downgrading to 0.17.6. fixed it. The changelog of 0.17.7 has this:
EDIT: It seems that 0.17.7 did something to the task headers. Tenant-schemas-celery adds the tenant schema name to the headers, so these changes probably somehow broke the headers with custom Task class: 0.17.6...0.17.7#diff-bbcc458d29b0f1880cf429abbf8a94a5e8517f61072945d783e44fec4969a10bL96 |
With Celery==4.2.2. I can try to provide a testapp but it will take a couple of weeks until I have time to try it out. The tasks that were failing were using:
|
I pinned to 0.17.7 and tracing started working again |
I have picked this up and I was able to replicate the issue and it seems that the issue occurs with the older / deprecated Task class in I believe we are only patching the new Task class Since invoking |
This issue has gone three weeks without activity. In another week, I will close it. But! If you comment or otherwise update it, I will reset the clock, and if you label it "A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀 |
It looks like the problem here is still open. |
This issue has gone three weeks without activity. In another week, I will close it. But! If you comment or otherwise update it, I will reset the clock, and if you label it "A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀 |
I believe this is still happening
on celery task with @periodic_task decorator. After
Are there any remediation for this? Pinning the version to |
@sl0thentr0py, is there a fix planned for this? |
Hello @abhishek-choudhury-callhub ! |
Hi @antonpirker! Can we have an estimate of when the fix can be deployed? |
@abhishek-choudhury-callhub there are multiple problems in this issue
which one are you interested in? this issue is very old, so please give me exact versions and the problem you are specifically facing. |
so I finally spent some time trying to repro this today, but was unable to. https://github.com/sl0thentr0py/sentry_sample_apps/tree/main/python/django celery is on Let me know how I can repro what you are seeing if you want me to investigate this further. |
@sl0thentr0py basically setup 2 celery tasks, one that uses |
@fredbaa could you send us a minimal project that can reproduce this? |
This issue has gone three weeks without activity. In another week, I will close it. But! If you comment or otherwise update it, I will reset the clock, and if you label it "A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀 |
Any chance this will be fixed? |
please see the discussion above and give me something to reproduce, there is nothing to fix otherwise. |
I'll try to make minimal project for repro purposes when I have the time, but so far this PR fixed problem for me: So the issue is real. |
I too have the run across the same issue. I upgraded sentry and suddenly had all kinds of issues with Tasks. Here is the exception, and I resolved it via the patch in this (PR) commit:
|
Cross posting here (from the linked PR I just closed) so that everybody is on the same page: I again tried to reproduce the problems described here and could not reproduce it. I also read the celery source and Sentry is currently patching This class is there in Celery 3-5: Your patch would monkey patch I am sorry, but I will close this issue/PR. If there is still something really breaking then please create a new issue with detailed information about versions of Sentry SDK and Celery and an example on how to reproduce it. |
I am using tenant-schemas-celery package, which generates it's own TenantTask (extends celery.app.task.Task) class in it's own CeleryApp (extends celery.Celery). After updating from sentry-sdk from 0.17.7 to 0.17.8 the TenantTask's tenant context switching stopped working. I suspect this is because of the following change in 0.17.8:
I think this problem has to do with this new way of importing celery.app.task.Task. Even though TenantTask extends the celery.app.task.Task, for some reason this change broke the TenantTask logic.
I'm not sure which package this should be fixed in, but I'm bit skeptical about this new import in sentry-sdk, so I'm reporting it here.
Here is my celery.py:
The text was updated successfully, but these errors were encountered: