Opened 17 years ago
Closed 17 years ago
#6950 closed (fixed)
django.template.loader is not thread-safe
Reported by: | bear330 | Owned by: | nobody |
---|---|---|---|
Component: | Template system | Version: | dev |
Severity: | Keywords: | template thread | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
In multithreaded environment, the django.template.loader is not thread-safe.
If browser sends many requests to django, sometimes it will raise TemplateDoesNotExist exception even the template actually existed.
This exception only raises in first requests, after that, everything will be fine.
This is because the django.template.loader use the global variable 'template_source_loaders'.
If many threads try to load template simultaneously, 'template_source_loaders' will not be initialized correctly.
I made a simple patch for that to fix this bug.
Thanks.
Attachments (3)
Change History (6)
by , 17 years ago
Attachment: | loader.patch added |
---|
comment:1 by , 17 years ago
An improved patch based on the findings of threading review. Initializes a temporary list instead of modifying the global list and assigns the temporary list in one "atomic" operation to the global list.
More to come to other parts of the template system.
by , 17 years ago
Attachment: | threadsafe_template_loader.diff added |
---|
by , 17 years ago
Attachment: | threadsafe_template_loader-const-correct.diff added |
---|
minor stylistic improvements (tuple instead of list for const-correctness)
comment:2 by , 17 years ago
Triage Stage: | Unreviewed → Ready for checkin |
---|
Marking as ready for checkin -- the last patch directly mimics source:django/trunk/django/template/context.py@7415#L86 , so it is the already prevalent way of doing things.
comment:3 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
A simple patch to make sure template_source_loaders is initialized correctly.