question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

request view resolution is broken when using an ASGI servers with subfolder tenants

See original GitHub issue

This initially came up while trying to setup django-tenants in SubFolder mode with an existing project that also uses django channels.

Accessing a tenant url would lead to the following error while processing any request under a tenant subpath :

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 472, in thread_handler
    raise exc_info[1]
  File "/usr/local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 42, in inner
    response = await get_response(request)
  File "/usr/local/lib/python3.9/site-packages/django/core/handlers/base.py", line 235, in _get_response_async
    callback, callback_args, callback_kwargs = self.resolve_request(request)
  File "/usr/local/lib/python3.9/site-packages/django/core/handlers/base.py", line 313, in resolve_request
    resolver_match = resolver.resolve(request.path_info)
  File "/usr/local/lib/python3.9/site-packages/django/urls/resolvers.py", line 651, in resolve
    sub_match = pattern.resolve(new_path)
  File "/usr/local/lib/python3.9/site-packages/django/urls/resolvers.py", line 646, in resolve
    match = self.pattern.match(path)
  File "/code/examples/tenant_subfolder_tutorial/../../django_tenants/urlresolvers.py", line 50, in match
    tenant_prefix = self.tenant_prefix
  File "/code/examples/tenant_subfolder_tutorial/../../django_tenants/urlresolvers.py", line 33, in tenant_prefix
    domain=connection.tenant.domain_subfolder,

Exception Type: AttributeError at /clients/test/
Exception Value: 'FakeTenant' object has no attribute 'domain_subfolder'

I was able to track this down and reproduce the issue with the example tenant_subfolder_tutorial. As soon as the project is served behind an ASGI server, the previous error occurs when accessing a subfolder tenant.

Steps to reproduce :

image

Preliminary analysis :

At this moment I think this boils down to the fact that we can’t rely on the connection object staying the same during the whole processing of the request. In WSGI mode I guess we can rely on 1 request = 1 connection because there will be one thread dedicated to servicing each request. In ASGI mode it seems this does not hold anymore.

Although this came up with the SubFolder Middleware, this might be a more general issue of pinning data on the Connection object in django_tenants middlewares

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ey3ballcommented, Aug 29, 2022

Per the comment above I don’t think importing connection into tenant_prefix would work because of the Local(thread_critical=True), relying directly on asgiref,local seems cleaner since we have control over the sharing flags instead of relying on whatever magic the connection class performs

1reaction
ey3ballcommented, Aug 26, 2022

I’ve tried the approach above (WIP patch here : https://github.com/ey3ball/django-tenants/commit/b995f77f1b4f0a4d77f945a913aecbe1a58a6de3), this seem to confirm what I’ve discovered so far, with this patch I can successfully acces the homepage of my tenant in subfolder mode with an asgi frontend.

Some more test required, I guess my approach might not support reverse() at the moment since I’m not actually registering the resulting urlconf module anywhere

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · django-tenants/django-tenants - GitHub
Contribute to django-tenants/django-tenants development by creating an account on ... request view resolution is broken when using an ASGI servers with ...
Read more >
Untitled
Xylograph plugin mac, Amanat ali faisalabad wiki, Top 10 green day songs 2012, Tenants board nsw, Open rhye mk remix, Apologise or apologize...
Read more >
Build a scalable security practice with Azure Lighthouse and Azure ...
Threat hunting and investigation through cross-tenant queries ... Let's see below example laravel convert data to json example. You can use this example ......
Read more >
Untitled
... ://www.digitalocean.com/community/questions/health-check-after-deployment ... .com/community/questions/i-need-to-access-openvpn-server-through-internet ...
Read more >
mutableState" in android jetpack compose? - t.co / Twitter
Note: You can also use property delegates as a syntactic sugar to unwrap the MutableState . var state: Int by remember { mutableStateOf(1)...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found