Multiple projects with different TIME_ZONE settings under mod_python can cause unexpected date/time behaviour
|Reported by:||durdinator||Owned by:||nobody|
|Severity:||Normal||Keywords:||mod_python time timezone|
|Cc:||me@…, nreilly@…||Triage Stage:||Accepted|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||yes|
When running Django under mod_python, if there are multiple projects running with different
TIME_ZONE settings, this can under some circumstances lead to the time zone (and hence the value returned by
datetime.datetime.now()) being different for subsequent requests to the same URL.
We encountered this problem running Apache 2.0 with the prefork MPM, with 10 subprocesses. While most of the numerous Django projects on the server had
TIME_ZONE = 'Europe/London' in the settings module, two had inadvertently been left with
TIME_ZONE = 'America/Chicago'. After investigating a problem where a time-based query would return unexpectedly different results for repeated requests, we found that
datetime.datetime.now() was mostly returning the local time value (10.05 am), but occasionally returning the wrong time (4.05 am). This 6 hour difference, not coincidentally, is the current difference between the America/Chicago and Europe/London time zones. Another instance of this problem was described in http://groups.google.com/group/django-users/browse_thread/thread/b2f8722f58b270fd/
Since the environment is maintained for each Apache subprocess, modifying it via
os.environ in one subinterpreter causes the modified environment to be visible in other subinterpreters, which may be serving a different site. This causes a problem at present, as
os.environ['TZ'] is set only once per project, when the settings are first loaded: http://code.djangoproject.com/browser/django/trunk/django/conf/__init__.py#L116
TZ environment variable were set per request (as is done with DJANGO_SETTINGS_MODULE and other environment variables passed through from the apache config via
SetEnv commands), then the correct time zone setting would be available for each project. I have attached a patch against SVN HEAD to do this in the mod_python handler.
Until this is corrected, a workaround is:
- Ensure that all django projects running under mod_python are using the same TIME_ZONE setting
- Or if different time zones are required, then ensure that the apache config uses SetEnv to set the correct TZ variable for every project.