jmcarp / nplusone

Auto-detecting the n+1 queries problem in Python
MIT License
1.01k stars 50 forks source link

maximum recursion depth exceeded in comparison #44

Open CosimoRusso opened 2 years ago

CosimoRusso commented 2 years ago

There are some cases when in the function signalify_fetch_all the decorator is called on the already decorated function queryset._fetch_all, causing a recursion depth exception if a fetch all query is invoked many times. This also happens if the query is not actually executed but its cached result is used. The issue can be reproduced through the following Django test:

class TestTheIssue(TestCase):
    def test_n_plus_one_wrapper_error(self):
        out = []
        # MyModelA has many MyModelB, the relationship is represented adding a 
        # models.ForeignKey field on model B with related name 'my_model_bs'
        qs = MyModelA.objects.all().prefetch_related('my_model_bs').first()
        # Execute this query N times, with N any number that is bigger than the recursion limit, 
        # in my case it is sys.getrecursionlimit() = 1000 
        for i in range(2000):  
            tmp = qs.my_model_bs.all()
            out.append(tmp.first())

        print(out[-1])

Details

nplusone version: 1.0.0 Django version: 3.2.13

environment: python:3.8.9-slim-buster image for Docker

johnnymetz commented 2 years ago

I'm also running into this issue.