getsentry / sentry-python

The official Python SDK for Sentry.io
https://sentry.io/for/python/
MIT License
1.88k stars 494 forks source link

Recursive serializer stackoverflow error when serializing an object that logs on iteration #469

Closed alexmick closed 5 years ago

alexmick commented 5 years ago

I'm currently having a problem with the SDKs integrated serialisation of local variables for sending with events. I have in my local variables, an object that is a Mapping but that sometimes raises an event when keys are accessed. This is a normal behavior, since keys are lazy loaded and my code handles these correctly. However, when an event occurs and this variable is serialized for sending to sentry, the sdk iterates over all my keys and causes an infinite recursion.

Minimal code te reproduce :

import logging
from collections.abc import Sequence
import sentry_sdk

logger = logging.getLogger(__name__)

class MyGenerator(Sequence):
    def __init__(self):
        self.values = [1, 2, 3]
        Sequence.__init__(self)

    def __iter__(self):
        for value in self.values:
            yield value
        logger.error(f"No values left", exc_info=True)

    def __len__(self):
        """List length"""
        return len(self.values)

    def __getitem__(self, ii):
        """Get a list item"""
        return self.values[ii]

if __name__ == "__main__":
    sentry_sdk.init(
        dsn="[DSN]"
    )

    gen = MyGenerator()

    logger.error("Testing", exc_info=True)

The only quick solution I found is to set with_locals=False on the sdk but it's a pity to lack all this functionality because of this case. On the other hand, I'm not sure how the SDK could anticipate such problems, here's what I'm thinking:

Happy to open a PR to follow-up on this when a proper solution is found !

untitaker commented 5 years ago

Thanks, this will be fixed with #470. All events captured during serialization are disregarded. The logs still show up in breadcrumbs.

There is already a semi-private hook called global_repr_processors that you can use to customize serialization of dangerous objects.