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.

AttributeError: 'coroutine' object has no attribute 'has_header' (Django 3.1)

See original GitHub issue

Hi,

I cannot use this middleware with an ASGI server. I tried with Daphne and Uvicorn, they both give me the following error:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.7/site-packages/django/utils/deprecation.py", line 116, in __call__
    response = self.process_response(request, response)
  File "/usr/local/lib/python3.7/site-packages/corsheaders/middleware.py", line 111, in process_response
    patch_vary_headers(response, ["Origin"])
  File "/usr/local/lib/python3.7/site-packages/django/utils/cache.py", line 286, in patch_vary_headers
    if response.has_header('Vary'):
AttributeError: 'coroutine' object has no attribute 'has_header'

Steps to reproduce:

  1. pip install django==3.1 daphne
  2. django-admin startproject example
  3. Add corsheaders in INSTALLED_APPS
  4. Add corsheaders.middleware.CorsMiddleware in MIDDLEWARE
  5. daphne example.asgi:application
  6. curl 127.0.0.1:8000 (in another terminal)

Best regards, Benoit

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
kordian-kowalskicommented, Aug 21, 2020

After investigating the issue, I found out that Django seems to pass a coroutine instead of the response to the first middleware’s process_response on the list, which usually ends up being this corsheaders. All other middlewares receive a response object. I’m not sure if this is by design or a bug in Django. Creating a dummy middleware that does nothing and placing it in front of corsheaders seems to “solve” the issue.

1reaction
kordian-kowalskicommented, Aug 21, 2020

@xtof-osd This is more of a workaround: https://docs.djangoproject.com/en/3.1/topics/async/#async-views

You will only get the benefits of a fully-asynchronous request stack if you have no synchronous middleware loaded into your site. If there is a piece of synchronous middleware, then Django must use a thread per request to safely emulate a synchronous environment for it.

but if someone’s in a pinch this can be done in settings.py right now: (see my other comment below)

import corsheaders.middleware
corsheaders.middleware.CorsMiddleware.async_capable = False
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to correct " 'coroutine' object has no attribute 'data'" Error ...
I keep getting an error that I cannot solve and for which I do not understand the reason. "'coroutine' object has no attribute...
Read more >
'coroutine' object has no attribute 'has_header' (Django 3.1)
AttributeError : 'coroutine' object has no attribute 'has_header' (Django 3.1)
Read more >
AttributeError: 'coroutine' object has no attribute 'error' - Reddit
I have .error events for my commands but they do not seem to work and show AttributeError: 'coroutine' object has no attribute 'error'....
Read more >
Mastering Telethon — Telethon 1.7.7 documentation
AttributeError : 'coroutine' object has no attribute 'id'. You probably had a previous version, upgraded, and expected everything to work.
Read more >
sys:1: runtimewarning: coroutine 'main' was never awaited
Note that simply calling a coroutine will not schedule it to be executed ... GUI raised exception: AttributeError("'SilentTaskGroup' object has no attribute ......
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