Django

Code

Ticket #6950 (new)

Opened 1 month ago

Last modified 1 month ago

django.template.loader is not thread-safe

Reported by: bear330 Assigned to: nobody
Component: Template system Version: SVN
Keywords: template thread Cc:
Triage Stage: Ready for checkin Has patch: 1
Needs documentation: 0 Needs tests: 0
Patch needs improvement: 0

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

loader.patch (0.6 kB) - added by bear330 on 04/03/08 04:47:25.
A simple patch to make sure template_source_loaders is initialized correctly.
threadsafe_template_loader.diff (1.1 kB) - added by mrts on 04/14/08 03:17:58.
threadsafe_template_loader-const-correct.diff (1.0 kB) - added by mrts on 04/14/08 14:17:32.
minor stylistic improvements (tuple instead of list for const-correctness)

Change History

04/03/08 04:47:25 changed by bear330

  • attachment loader.patch added.

A simple patch to make sure template_source_loaders is initialized correctly.

04/14/08 03:17:23 changed by mrts

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

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.

04/14/08 03:17:58 changed by mrts

  • attachment threadsafe_template_loader.diff added.

04/14/08 14:17:32 changed by mrts

  • attachment threadsafe_template_loader-const-correct.diff added.

minor stylistic improvements (tuple instead of list for const-correctness)

04/16/08 06:12:36 changed by mrts

  • stage changed from Unreviewed to 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.


Add/Change #6950 (django.template.loader is not thread-safe)




Change Properties
Action