#26306: Memory leak in cached template loader
---------------------------------+--------------------
     Reporter:  AlexHill         |      Owner:  nobody
         Type:  Bug              |     Status:  new
    Component:  Template system  |    Version:  1.9
     Severity:  Normal           |   Keywords:
 Triage Stage:  Unreviewed       |  Has patch:  0
Easy pickings:  0                |      UI/UX:  0
---------------------------------+--------------------
 There's a pretty serious memory leak in the cached template loader.

 Here's a bare-bones project demonstrating the issue:
 https://github.com/AlexHill/django_leak_test

 To see it happen, run that project under 1.9, hit /safe/ a couple thousand
 times, then try the same with /leaky/.

 Here's where it happens:
 
https://github.com/django/django/blob/6679cdd92c71a77f1809c180174de026a6c17918/django/template/loaders/cached.py#L34

 I think what's happening is this:

 When the exception is raised, it contains a stack trace, which contains
 references to a whole lot of stuff up the stack including the
 TemplateResponse, the request, etc. That's all cached with the exception.
 When it's fetched from the cache and re-raised, Python's exception
 handling mechanics result in the exception containing references to the
 current stack AND the previous one.

 This happens every time it's fetched from cache, so essentially you end up
 caching the response and context for every request that produced a
 TemplateDoesNotExist error while resolving a template name. In my case,
 that's basically every request :)

 I think caching and re-raising the actual exception is probably a mistake
 - instead, we should cache some sentinel object and raise a new exception
 each time.

--
Ticket URL: <https://code.djangoproject.com/ticket/26306>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/051.a98508b189629e0e346d6c18fb0d7e2b%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to