Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
orm: queryset: Fix circular reference error #4247
In my further development of the ORM, I found this bug. It's a circular reference between the ModelInstanceManager and the QuerySet instances. It prevents the query statement and database resources from being cleaned up automatically when the query leaves scope (ie. function exits) and there are still records to be fetched from the database (for instance, if only the first record of a recordset with multiple rows was fetched).
This fixes an error where the ModelInstanceManager maintained a reference to the QuerySet instance, and the QuerySet instance managed a reference to the ModelInstanceManager instance (if it's the iterator for the query). Because of the circular reference, if the iterator is not exhausted, then the resource is not closed and the query remains open. This wastes memory and prevents some other queries from running after such a situation happens.
This addresses the issue by removing the circular reference between the QuerySet and the ModelInstanceManager.