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

The middleware doesn't work properly in asgi mode #381

Closed
CrazyUmka opened this issue Oct 20, 2023 · 2 comments · Fixed by #382
Closed

The middleware doesn't work properly in asgi mode #381

CrazyUmka opened this issue Oct 20, 2023 · 2 comments · Fixed by #382

Comments

@CrazyUmka
Copy link

Python Version

3.12.0

Django Version

4.2.6

Package Version

1.17.0

Description

When you turn on asgi mode in Django using daphne we get error message in middleware chain. This happens because Django uses the following function asgi.sync.markcoroutinefunction to mark whether a coroutine is present or not. In here

self._is_coroutine = (
instead of _is_coroutine there should be an attribute _is_coroutine_marker. To fix this, you can use the markcoroutinefunction or the MiddlewareMixin class to support backwards compatibility.

Example of configuration to reproduce behavior

INSTALLED_APPS = [
  "daphne",
  "django.contrib.admin",
  "django.contrib.auth",
  "django.contrib.contenttypes",
  "django.contrib.sessions",
  "django.contrib.messages",
  "django.contrib.staticfiles",
  "django_htmx",
]
MIDDLEWARE = [
  "django.middleware.security.SecurityMiddleware",
  "django.contrib.sessions.middleware.SessionMiddleware",
  "django.middleware.common.CommonMiddleware",
  "django.middleware.csrf.CsrfViewMiddleware",
  "django.contrib.auth.middleware.AuthenticationMiddleware",
  "django.contrib.messages.middleware.MessageMiddleware",
  "django.middleware.clickjacking.XFrameOptionsMiddleware",
  "django_htmx.middleware.HtmxMiddleware",
]

After that, you get next error: object HttpResponse can't be used in 'await' expression

@CrazyUmka CrazyUmka changed the title The HtmxMiddleware doesn't work properly in asgi mode The middleware doesn't work properly in asgi mode Oct 20, 2023
@adamchainz
Copy link
Owner

adamchainz commented Oct 22, 2023

Thank you for the report. I haven't been able to reproduce the problem because you didn’t provide enough details, but I have a proposed fix in #382. Please can you test it?

When you turn on asgi mode in Django using daphne we get error message in middleware chain.

Please provide the error message with the stack trace.

After that, you get next error: object HttpResponse can't be used in 'await' expression

Please provide the stack trace.

instead of _is_coroutine there should be an attribute _is_coroutine_marker. To fix this, you can use the markcoroutinefunction

I see this changed in Python 3.12. Thanks.

or the MiddlewareMixin class to support backwards compatibility.

The middleware deliberately doesn't use that class, which would make it slower because it uses sync_to_async.

Python Version

3.12.0

Django Version

4.2.6

Django 4.2.6 doesn’t currently support Python 3.12. The only supported versions are those listed in the release notes page: https://docs.djangoproject.com/en/4.2/releases/4.2/ . The tracking ticket: https://code.djangoproject.com/ticket/34118 . You will likely encounter other incompatibility issues, especially so early into Python 3.12's release cycle.

@adamchainz
Copy link
Owner

I also made an optimization PR on Django, following investigation into the changes to MiddlewareMixin: django/django#17402 .

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 a pull request may close this issue.

2 participants