8000 Celery integration not capturing error with max_tasks_per_child = 1 · Issue #285 · getsentry/sentry-python · GitHub
[go: up one dir, main page]

Skip to content
Celery integration not capturing error with max_tasks_per_child = 1 #285
Closed
@okomarov

Description

@okomarov

The celery integration is failing to capture the exception when I use a celery factory pattern which patches the celery task with Flask's context.

This is web/celery_factory.py

# Source: https://stackoverflow.com/questions/12044776/how-to-use-flask-sqlalchemy-in-a-celery-task

from celery import Celery
import flask


class FlaskCelery(Celery):

    def __init__(self, *args, **kwargs):
        super(FlaskCelery, self).__init__(*args, **kwargs)
        self.patch_task()

        if 'app' in kwargs:
            self.init_app(kwargs['app'])

    def patch_task(self):
        TaskBase = self.Task
        _celery = self

        class ContextTask(TaskBase):
            abstract = True

            def __call__(self, *args, **kwargs):
                if flask.has_app_context():
                    return TaskBase.__call__(self, *args, **kwargs)
                else:
                    with _celery.app.app_context():
                        return TaskBase.__call__(self, *args, **kwargs)

        self.Task = ContextTask

    def init_app(self, app):
        self.app = app
        self.config_from_object(app.config)


celery_app = FlaskCelery()

I am adding a random raise inside a simple task

import celery_app from celery_factory.py
@celery_app.task
def simple_task():
    raise Exception("Testing Celery exception")

The error I get printed is:

[2019-03-08 21:24:21,117: ERROR/ForkPoolWorker-31] Task simple_task[d6e959b1-7253-4e55-861d-c1968ae14e1c] raised unexpected: RuntimeError('No active exception to reraise')
Traceback (most recent call last):
  File "/Users/okomarov/.virtualenvs/myenv/lib/python3.7/site-packages/celery/app/trace.py", line 382, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/Users/okomarov/Documents/repos/myproject/web/celery_factory.py", line 28, in __call__
    return TaskBase.__call__(self, *args, **kwargs)
  File "/Users/okomarov/.virtualenvs/myenv/lib/python3.7/site-packages/celery/app/trace.py", line 641, in __protected_call__
    return self.run(*args, **kwargs)
  File "/Users/okomarov/.virtualenvs/myenv/lib/python3.7/site-packages/sentry_sdk/integrations/celery.py", line 66, in _inner
    reraise(*_capture_exception())
  File "/Users/okomarov/.virtualenvs/myenv/lib/python3.7/site-packages/sentry_sdk/_compat.py", line 52, in reraise
    raise value
  File "/Users/okomarov/.virtualenvs/myenv/lib/python3.7/site-packages/sentry_sdk/integrations/celery.py", line 64, in _inner
    return f(*args, **kwargs)
  File "/Users/okomarov/.virtualenvs/myenv/lib/python3.7/site-packages/sentry_sdk/integrations/celery.py", line 66, in _inner
    reraise(*_capture_exception())
  File "/Users/okomarov/.virtualenvs/myenv/lib/python3.7/site-packages/sentry_sdk/_compat.py", line 52, in reraise
    raise value
  File "/Users/okomarov/.virtualenvs/myenv/lib/python3.7/site-packages/sentry_sdk/integrations/celery.py", line 64, in _inner
    return f(*args, **kwargs)
  File "/Users/okomarov/Documents/repos/myproject/web/simple_task.py", line 4, in simple_task
    raise Exception("Testing Celery exception")
RuntimeError: No active exception to reraise

Relevant pip packages:

Celery==4.2.1
Flask==1.0.2
sentry-sdk==0.7.4

The integration is called as following (flask integration works as expected):

from flask import Flask
from celery_factory import celery_app
from config import config_to_use


def create_app():
    app = Flask()
    app.config.from_object(config_to_use)

    init_logging(app)

    register_extensions(app)
    register_blueprints(app)
    register_jinja_extras(app)

    return app


def init_logging(app):
    import sentry_sdk
    from sentry_sdk.integrations.flask import FlaskIntegration
    from sentry_sdk.integrations.celery import CeleryIntegration

    sentry_sdk.init(
        dsn=app.config.get('FLASK_SENTRY_DSN'),
        integrations=[FlaskIntegration(), CeleryIntegration()]
        )

...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0