ASGI Django with OpencensusMiddleware erroring on all requests with TypeError: object HttpResponse can't be used in 'await' expression
Before anything else, thanks to everyone working on this project and anyone who spends time looking at this issue. :slightly_smiling_face:
I've created this issue as a bug because my understanding of the issue is that opencensus-ext-django does not support ASGI configurations of Django, but it's possible that I could have misconfigured something.
Describe your environment.
Using Python 3.8.11, Django 3.2.8, opencensus-ext-django 0.7.6, Channels 3.0.4. Running locally inside of Docker image development environment (VS Code Dev Container). Deploying to an Azure App Service, OpencensusMiddleware required for an Azure Application Insights resource.
Repo that reproduces the error: https://github.com/oscarhermoso/bug-opentelemetry-django-asgi
Full requirements.txt
asgiref==3.4.1
attrs==21.2.0
autobahn==21.3.1
Automat==20.2.0
azure-core==1.19.0
azure-identity==1.6.1
cachetools==4.2.4
certifi==2021.10.8
cffi==1.14.6
channels==3.0.4
charset-normalizer==2.0.6
constantly==15.1.0
cryptography==35.0.0
daphne==3.0.2
Django==3.2.8
django-admin==2.0.1
django-excel-base==1.0.4
django-excel-response2==3.0.2
django-six==1.0.4
google-api-core==2.1.0
google-auth==2.3.0
googleapis-common-protos==1.53.0
hyperlink==21.0.0
idna==3.2
incremental==21.3.0
msal==1.15.0
msal-extensions==0.3.0
opencensus==0.8.0
opencensus-context==0.1.2
opencensus-ext-azure==1.1.0
opencensus-ext-django==0.7.6
portalocker==1.7.1
protobuf==3.18.1
psutil==5.8.0
psycopg2==2.9.1
pyasn1==0.4.8
pyasn1-modules==0.2.8
pycparser==2.20
PyJWT==2.2.0
pyOpenSSL==21.0.0
pytz==2021.3
requests==2.26.0
rsa==4.7.2
screen==1.0.1
service-identity==21.1.0
six==1.16.0
sqlparse==0.4.2
Twisted==21.7.0
txaio==21.2.1
typing-extensions==3.10.0.2
urllib3==1.26.7
xlwt==1.3.0
zope.interface==5.4.0
Steps to reproduce.
Run Django server in ASGI configuration, and make a request to the server.
https://github.com/oscarhermoso/bug-opentelemetry-django-asgi
What is the expected behavior?
Expect server to respond to requests as normal, browser to display successful response, and OpencensusMiddleware to provide traces for requests.
What is the actual behavior? What did you see instead?
Browser renders this error:
500 Internal Server Error
Exception inside application.
Daphne
Terminal logs this traceback:
Internal Server Error: /
Traceback (most recent call last):
File "/home/vscode/.local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/vscode/.local/lib/python3.8/site-packages/django/utils/deprecation.py", line 119, in __call__
response = self.process_response(request, response)
File "/home/vscode/.local/lib/python3.8/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'
Exception inside application: object HttpResponse can't be used in 'await' expression
Traceback (most recent call last):
File "/home/vscode/.local/lib/python3.8/site-packages/channels/staticfiles.py", line 44, in __call__
return await self.application(scope, receive, send)
File "/home/vscode/.local/lib/python3.8/site-packages/channels/routing.py", line 71, in __call__
return await application(scope, receive, send)
File "/home/vscode/.local/lib/python3.8/site-packages/django/core/handlers/asgi.py", line 161, in __call__
response = await self.get_response_async(request)
File "/home/vscode/.local/lib/python3.8/site-packages/django/core/handlers/base.py", line 150, in get_response_async
response = await self._middleware_chain(request)
TypeError: object HttpResponse can't be used in 'await' expression
HTTP GET / 500 [0.68, 127.0.0.1:36658]
Additional context.
- This issue wasn't occurring on the previous version of Django (3.1.13)
- Even though the trace-back seems unrelated, if I remove the OpenCensus middleware from the Django settings then the issue does not occur. Similarly, if I remove Django Channels from the project, then the issue also doesn't occur.
- Someone on the Django discord suggested adding Sentry to provide additional debugging details... adding a basic sentry config to
settings.pysomehow resolves the issue?- This video shows the error occurring, followed by adding a Sentry config that somehow resolves the problem https://www.youtube.com/watch?v=MLBDVw6IMPA
Made a stackoverflow post for this one too:
https://stackoverflow.com/questions/69506127/django-daphne-500-error-on-all-requests-after-version-update-typeerror-obje/69597418#69597418