question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Version 0.17.8 broke Celery tasks with custom Task class

See original GitHub issue

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:

diff --git a/sentry_sdk/integrations/celery.py b/sentry_sdk/integrations/celery.py
index 1a11d4a..2b51fe1 100644
--- a/sentry_sdk/integrations/celery.py
+++ b/sentry_sdk/integrations/celery.py
@@ -60,9 +60,8 @@ class CeleryIntegration(Integration):
                 # Need to patch both methods because older celery sometimes
                 # short-circuits to task.run if it thinks it's safe.
                 task.__call__ = _wrap_task_call(task, task.__call__)
                 task.run = _wrap_task_call(task, task.run)
-                task.apply_async = _wrap_apply_async(task, task.apply_async)

                 # `build_tracer` is apparently called for every task
                 # invocation. Can't wrap every celery task for every invocation
                 # or we will get infinitely nested wrapper functions.
@@ -71,8 +70,12 @@ class CeleryIntegration(Integration):
             return _wrap_tracer(task, old_build_tracer(name, task, *args, **kwargs))

         trace.build_tracer = sentry_build_tracer

+        from celery.app.task import Task  # type: ignore
+
+        Task.apply_async = _wrap_apply_async(Task.apply_async)
+
         _patch_worker_exit()

         # This logger logs every status of every task that ran on the worker.
         # Meaning that every task's breadcrumbs are full of stuff like "Task

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:

from tenant_schemas_celery.app import CeleryApp as TenantAwareCeleryApp


app = TenantAwareCeleryApp()
app.config_from_object('django.conf:settings')
app.autodiscover_tasks()

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:29 (12 by maintainers)

github_iconTop GitHub Comments

3reactions
clintmodcommented, Nov 27, 2020

I pinned to 0.17.7 and tracing started working again

2reactions
shumakoffcommented, Oct 10, 2022

I’ll try to make minimal project for repro purposes when I have the time, but so far this PR fixed problem for me: https://github.com/getsentry/sentry-python/pull/1151

So the issue is real.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tasks — Celery 5.2.7 documentation
Task.retry() ), for accessing information about the current task request, and for any additional functionality you add to custom task base classes.
Read more >
Celery Flower - tasks not shown when I define tasks by custom ...
I found a reason, when I execute a task, I need to specify the name of args (in this case, task_type ) explicitly....
Read more >
Celery + Redis + Django - Blog Post - Coding for Entrepreneurs
Recommended course: Time & Tasks 2. In this course, you'll learn how to implement Celery with Redis and Django leveraging a lot of...
Read more >
Executing Tasks — Celery 2.6.0rc4 documentation
In addition you can set countdown/eta, task expiry, provide a custom broker connection and more. Let's go over these in more detail. All...
Read more >
Celery tasks are broken - Site Operations Help
Hi all, I'm hoping somebody can help me. I seem to be running into exactly this unsolved problem. When I try to export...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found