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

Incompatible with Daphne under ASGI #908

Closed
1 task done
captain828 opened this issue Nov 5, 2023 · 7 comments
Closed
1 task done

Incompatible with Daphne under ASGI #908

captain828 opened this issue Nov 5, 2023 · 7 comments

Comments

@captain828
Copy link

captain828 commented Nov 5, 2023

Understanding CORS

  • I have read the resources.

Python Version

3.12

Django Version

5.0b1

Package Version

4.3.0

Description

Getting errors on Django 5 + Daphne ASGI:

Internal Server Error: /
Traceback (most recent call last):
  File "[root]\venv\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "[root]\venv\Lib\site-packages\django\utils\deprecation.py", line 136, in __call__
    response = self.process_response(request, response)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "[root]\venv\Lib\site-packages\django\middleware\security.py", line 47, in process_response
    response.headers.setdefault("X-Content-Type-Options", "nosniff")
    ^^^^^^^^^^^^^^^^
AttributeError: 'coroutine' object has no attribute 'headers'
Exception inside application: object HttpResponse can't be used in 'await' expression
Traceback (most recent call last):
  File "[root]\venv\Lib\site-packages\django\contrib\staticfiles\handlers.py", line 101, in __call__
    return await self.application(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "[root]\venv\Lib\site-packages\django\core\handlers\asgi.py", line 170, in __call__
    await self.handle(scope, receive, send)
  File "[root]\venv\Lib\site-packages\django\core\handlers\asgi.py", line 209, in handle
    task.result()
  File "[root]\venv\Lib\site-packages\django\core\handlers\asgi.py", line 192, in process_request
    response = await self.run_get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "[root]\venv\Lib\site-packages\django\core\handlers\asgi.py", line 237, in run_get_response
    response = await self.get_response_async(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "[root]\venv\Lib\site-packages\django\core\handlers\base.py", line 162, in get_response_async
    response = await self._middleware_chain(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: object HttpResponse can't be used in 'await' expression
HTTP GET / 500 [0.73, 127.0.0.1:50708]

settings.py

INSTALLED_APPS = [
    'daphne',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'corsheaders',
    'apps.my_app',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    # 'django.middleware.csrf.CsrfViewMiddleware',
    # 'django.middleware.locale.LocaleMiddleware',
    # 'django.middleware.cache.UpdateCacheMiddleware',
    'django.middleware.common.CommonMiddleware',
    # 'django.middleware.cache.FetchFromCacheMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
    # 'django_brotli.middleware.BrotliMiddleware',
    # 'django.middleware.gzip.GZipMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
]

asgi.py - default from Django

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')

application = get_asgi_application()

runserver command

python manage.py runserver 127.0.0.1:8000

I tried removing as many variables as I could but the error only goes away when I remove the CORS middleware.

@adamchainz
Copy link
Owner

It looks like you're experiencing the same thing as this report in another of my packages: adamchainz/django-htmx#381 . Would you be able to check out that issue and related PR and maybe test them? Then you or I could make an identical PR here.

@captain828
Copy link
Author

I'll have a look but I can't promise I'll have time to do it this week.

@Royalflamejlh
Copy link
Contributor

This is likely affecting all ASGI servers, I'm having the same issue with uvicorn.

@daggaz
Copy link

daggaz commented Nov 17, 2023

This causes an incompatibility with asgiref < 3.6.0 as iscoroutinefunction was only added in this commit:
django/asgiref@36f37c9

Maybe update the dependencies to require at least 3.6.0?

@adamchainz
Copy link
Owner

That was done: 7fe1d62#diff-50c86b7ed8ac2cf95bd48334961bf0530cdc77b5a56f852c5c61b89d735fd711R46

What tooling did you use to install an incompatible asgiref with? Can you provide a reproduction?

@JPaulMora
Copy link

Bumping this thread, can't use channels with this package.

@adamchainz
Copy link
Owner

I think this was fixed already back in #911. If you're still having issues with Channels please open a new issue @JPaulMora .

Repository owner locked as resolved and limited conversation to collaborators Oct 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants