-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Set urlconf to None after changing SUBDOMAIN setting #4032
Set urlconf to None after changing SUBDOMAIN setting #4032
Conversation
Hmm. Does this happen in prod? We are setting the urlconf in middleware, but should we be unsetting it also? Or is Django reseting it during normal prod usage, but not in the test cases? |
Looks like Django resets it on Response, so in prod it will be reset here I believe: https://github.com/django/django/blob/3df8ccf6fc3fa0ab2acf9a03da43fea87f8ff392/django/core/handlers/base.py#L113 |
Still feels like this fix should probably go somewhere else, so that it doesn't keep happening across test cases. I wonder if we should be unsetting it in the middleware here, where it's set: https://github.com/rtfd/readthedocs.org/blob/master/readthedocs/core/middleware.py#L36 |
I'm not sure if this happens on prod, this can be replicated with any url_name (I was testing this with a generated url from rest_framework only), I just replicate this same behavior with So, at least we aren't using any reverse function, I think this only happens on tests. |
Yeah, I was thinking the same, but this was the only idea I was able to think, I going to take a deeper look to the code you linked and see if this can be fixed for all tests cases. |
4c13fda
to
c69eedb
Compare
This is what I found about
And this from a lib that changes the urlconf attr of the request But I think this behavior is different on the Django version that rtd uses according to the code that handle the middlewars |
c69eedb
to
6c0b545
Compare
I did some tests with this solution (restoring the urlconf attr when processing the response) and it works, but I'm not sure if this can break other stuff. Also, maybe we want to apply this solution to the others middlewares that modify |
Definitely this change a lot in django 1.10 https://github.com/django/django/blob/1.10/django/core/handlers/base.py#L54 |
I'll try to write some tests for this later |
8cbe8ed
to
3ea856b
Compare
ok, I managed to write a test for this, it fails if the new code is deleted |
This looks good to me, but does scare me a little bit. It will be pretty obvious if it breaks in prod though, so I think it makes sense to merge it. |
This makes sense to me. I pull down the branch, run the test with and without the changes and it worked as expected. It seems that it won't fail in production and I think we are ready to merge it. |
The urlconf value is preserved for the current thread, this causes errors when using the reverse function.
When the request is made inside that test, django overrides the url_conf (readthedocs.core.urls.subdomain) for the current thread! https://docs.djangoproject.com/en/1.9/_modules/django/core/urlresolvers/ (set_urlconf). So, all tests are running in that thread with that configuration, till the value get's override (to readthedocs.urls) doing another request without the HTTP_HOST header.
So, is this a bug on django or on the rtd code? I'm not sure, but adding this teardown method solves the issue.
You can reproduce this with this branch #4017 (comment)
Also this bug is affecting this other PR #3913 (comment)