﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
26306	Memory leak in cached template loader	Alex Hill	Alex Hill	"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."	Bug	closed	Template system	1.9	Release blocker	fixed		berker.peksag@…	Accepted	1	0	0	0	0	0
