Skip to content

CeleryIntegration wrongly reporting errors for tasks with custom __call__ method #1137

@nbrede

Description

@nbrede

Environment

How do you use Sentry?
Sentry SaaS (sentry.io)

Which SDK and version?
Python 1.1.0

Steps to Reproduce

  1. Create a custom celery base task class
  2. Implement a custom __call__ method on that base task class
  3. Catch and handle certain exceptions in that __call__ method
  4. Raise a different exception from the custom __call__ method

Example code:

from celery import Task

class CustomBaseTask(Task):
    def __call__(self, *args, **kwargs):
        try:
            super().__call__(*args, **kwargs)
        except CustomExceptionA:
            # do sth
            raise SpecialException()
        except CustomExceptionB:
            pass


@shared_task(base=CustomBaseTask, bind=True)
def some_actual_task(self, ...):
   ...  # do sth here

Expected Result

  • Errors caught in the custom __call__ method should not be sent to sentry (i.e., CustomExceptionA, CustomExceptionB)
  • Errors raised in the custom __call__ method should be sent to sentry (i.e., SpecialException)

Actual Result

  • CustomExceptionA and CustomExceptionB are shown by sentry as unhandled (although in fact they are as our other monitoring systems confirm)
  • SpecialException is never shown in sentry, although it is being raised (confirmed by our other monitoring systems)

From debugging it seems that sentry wraps the task's run method, which is called by the celery.Task.__call__ method and thus at a lower level than the custom call method.

Also it seems that sentry's attempted wrap of the task's __call__ method is not working, since SpecialException is never sent to sentry.

Metadata

Metadata

Assignees

No one assigned
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions