Skip to content
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

Django LazyObject are evaluated multiple times on exception #3854

Open
tefra opened this issue Dec 5, 2024 · 0 comments
Open

Django LazyObject are evaluated multiple times on exception #3854

tefra opened this issue Dec 5, 2024 · 0 comments

Comments

@tefra
Copy link

tefra commented Dec 5, 2024

How do you use Sentry?

Sentry Saas (sentry.io)

Version

2.19.0

Steps to Reproduce

When utilizing Sentry within a Django project, the integration seems to cause the function _setup() of a LazyObject to run multiple times when an exception is thrown within the method.

import os
from logging import getLogger

import django
from django.utils.functional import LazyObject

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings")
django.setup()

logger = getLogger(__name__)
count = 0

class MiLazy(LazyObject):
    def _setup(self):
        global count
        count += 1
        logger.info(count)
        raise ValueError(count)


foo = MiLazy()
len(foo)

It doesn't matter if I enable the DjangoIntegration or not in the settings, the issue goes away when I remove the sdk init completely.

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "handlers": {
        "console": {
            "level": "INFO",
            "class": "logging.StreamHandler",
        },
    },
    "loggers": {
        "": {
            "handlers": ["console"],
            "level": "INFO",
        },
    },
}

sentry_sdk.init(
    dsn=os.getenv("SENTRY_DSN"),
    environment="Production",
    integrations=[DjangoIntegration()],
)

Expected Result

1
Traceback (most recent call last):
  File "/path/foo.py", line 22, in <module>
    len(foo)
  File "/path/.venv/lib/python3.10/site-packages/django/utils/functional.py", line 251, in inner
    self._setup()
  File "/path/foo.py", line 18, in _setup
    raise ValueError(count)
ValueError: 1

Actual Result

1
2
3
Traceback (most recent call last):
  File "/path/foo.py", line 22, in <module>
    len(foo)
  File "/path/.venv/lib/python3.10/site-packages/django/utils/functional.py", line 251, in inner
    self._setup()
  File "/path/foo.py", line 18, in _setup
    raise ValueError(count)
ValueError: 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Waiting for: Product Owner
Development

No branches or pull requests

2 participants