Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for uninitialized thread-local SITE_ID #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

am0s
Copy link

@am0s am0s commented Feb 27, 2015

This fixes a problem with the thread-local SITE_ID variable being unmodified in some cases.
I've had issues with requests switching between the Site instances at seemingly random, by ensuring that the SITE_ID is always properly initialized we avoid this problem.

In addition I disabled the fallback code that picks the first Site item, picking a random site is not what you normally want.
I used a setting for it so it can be brought back if wanted.

am0s added 4 commits February 27, 2015 16:05
In some cases the thread-local variable ends up being unmodified, the
value will then be unpredictable as we may get one from a thread that
was previously used. Instead it is always reset to the default.
…uce a new setting for this package

Set the settings DOMAINS_ALLOW_DEFAULT_SITE_ID to the ID that should
be the default.
If the value is not set (or 0) it will not use the default site.
Picking a random site in the fallback is not necessarily a good idea.
If you still want the behaviour set DOMAINS_ALLOW_FALLBACK to True.
@marazmiki
Copy link
Owner

Hi, Jan

Did you cover your code with tests?

@marazmiki
Copy link
Owner

Can you reproduce your case, please? Please tell me scenario or (it's preferred) write some unittest cases?

@am0s
Copy link
Author

am0s commented Mar 25, 2015

Hi,

I don't think there is an easy way to write automated tests for this as it only occurs on a production setup, I've never had the problem with my development setup.
It also doesn't happen consistently, for instance after restarting my gunicorn setup everything works normally for a while until suddenly something triggers the problem.
My guess is that the same thread is re-used and the thread local variable stays intact from a previous request.

After adding several log entries to the django-domains code I was able to spot that a new request got the SITE_ID value from a previous request result.

The scenario I have is a site that uses multiple sub domains, each sub-domain maps to a database object which limits the request to certain subset of private data. With the SITE_ID bug I ended up with sub-domains pointing to the wrong subset, which is quite catastrophic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants