jazzband/django-hosts

Broken w/ Django 3.1 & ASGI Server

aaronn opened this issue · 5 comments

When running django-hosts with an asgi server like uvicorn you will run into the following error which appears to specifically be related to the django-hosts middleware.

This Django issue may be a clue for a fix.

Run command:
gunicorn -w 4 -k myproj.uvicorn.UvicornWorker myproj.asgi:application -b 0.0.0.0:10000

Traceback:

[2020-09-05 15:34:28 -0700] [93779] [INFO] Booting worker with pid: 93779
[2020-09-05 15:34:28 -0700] [93780] [INFO] Booting worker with pid: 93780
[2020-09-05 15:34:28 -0700] [93781] [INFO] Booting worker with pid: 93781
[2020-09-05 15:34:28 -0700] [93782] [INFO] Booting worker with pid: 93782
[2020-09-05 22:34:30 +0000] [93780] [INFO] Started server process [93780]
[2020-09-05 22:34:30 +0000] [93781] [INFO] Started server process [93781]
[2020-09-05 22:34:30 +0000] [93782] [INFO] Started server process [93782]
[2020-09-05 22:34:30 +0000] [93779] [INFO] Started server process [93779]
Internal Server Error: /users/self/
Traceback (most recent call last):
  File "/Users/james/Github/oregano-server/.venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/Users/james/Github/oregano-server/.venv/lib/python3.7/site-packages/django/utils/deprecation.py", line 116, in __call__
    response = self.process_response(request, response)
  File "/Users/james/Github/oregano-server/.venv/lib/python3.7/site-packages/django/middleware/clickjacking.py", line 26, in process_response
    if response.get('X-Frame-Options') is not None:
AttributeError: 'coroutine' object has no attribute 'get'
2020-09-05 22:35:03,901 ERROR Internal Server Error: /users/self/
Traceback (most recent call last):
  File "/Users/james/Github/oregano-server/.venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/Users/james/Github/oregano-server/.venv/lib/python3.7/site-packages/django/utils/deprecation.py", line 116, in __call__
    response = self.process_response(request, response)
  File "/Users/james/Github/oregano-server/.venv/lib/python3.7/site-packages/django/middleware/clickjacking.py", line 26, in process_response
    if response.get('X-Frame-Options') is not None:
AttributeError: 'coroutine' object has no attribute 'get'
Internal Server Error: /users/self/
Traceback (most recent call last):
  File "/Users/james/Github/oregano-server/.venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/Users/james/Github/oregano-server/.venv/lib/python3.7/site-packages/asgiref/sync.py", line 139, in __call__
    return call_result.result()
  File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/_base.py", line 428, in result
    return self.__get_result()
  File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
    raise self._exception
  File "/Users/james/Github/oregano-server/.venv/lib/python3.7/site-packages/asgiref/sync.py", line 204, in main_wrap
    result = await self.awaitable(*args, **kwargs)
TypeError: object HttpResponse can't be used in 'await' expression
2020-09-05 22:35:03,982 ERROR Internal Server Error: /users/self/
Traceback (most recent call last):
  File "/Users/james/Github/oregano-server/.venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/Users/james/Github/oregano-server/.venv/lib/python3.7/site-packages/asgiref/sync.py", line 139, in __call__
    return call_result.result()
  File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/_base.py", line 428, in result
    return self.__get_result()
  File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
    raise self._exception
  File "/Users/james/Github/oregano-server/.venv/lib/python3.7/site-packages/asgiref/sync.py", line 204, in main_wrap
    result = await self.awaitable(*args, **kwargs)
TypeError: object HttpResponse can't be used in 'await' expression

same issue

hi you can fix it by inheriting from middlewares

from django_hosts.middleware import HostsRequestMiddleware, HostsResponseMiddleware


class SyncHostsRequestMiddleware(HostsRequestMiddleware):
    async_capable = False
    sync_capable = True


class SyncHostsResponseMiddleware(HostsResponseMiddleware):
    async_capable = False
    sync_capable = True

hi you can fix it by inheriting from middlewares

from django_hosts.middleware import HostsRequestMiddleware, HostsResponseMiddleware


class SyncHostsRequestMiddleware(HostsRequestMiddleware):
    async_capable = False
    sync_capable = True


class SyncHostsResponseMiddleware(HostsResponseMiddleware):
    async_capable = False
    sync_capable = True

Where do you have to put this?

hi you can fix it by inheriting from middlewares

from django_hosts.middleware import HostsRequestMiddleware, HostsResponseMiddleware


class SyncHostsRequestMiddleware(HostsRequestMiddleware):
    async_capable = False
    sync_capable = True


class SyncHostsResponseMiddleware(HostsResponseMiddleware):
    async_capable = False
    sync_capable = True

Where do you have to put this?

@cronixx Create a middleware.py in your directory where settings.py is present and replace the HostsRequestMiddleware and HostsResponseMiddleware middlewares in settings.py with this new inherited middlewares.

This issue should now be resolved by #126 (which is included in version 5.1); please reopen the issue if that's not the case 🙂