uvicorn: Exception in ASGI application - 'NoneType' object is not subscriptable
Hello, I have ‘NoneType’ error on asgi_send method, im not sure for why this happens:
[2018-11-07 22:13:39 -0500] [2868] [ERROR] Exception in ASGI application
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/uvicorn/protocols/websockets/websockets_impl.py", line 140, in run_asgi
result = await asgi(self.asgi_receive, self.asgi_send)
File "/usr/local/lib/python3.6/dist-packages/channels/sessions.py", line 175, in __call__
return await self.inner(receive, self.send)
File "/usr/local/lib/python3.6/dist-packages/channels/middleware.py", line 41, in coroutine_call
await inner_instance(receive, send)
File "/usr/local/lib/python3.6/dist-packages/channels/consumer.py", line 54, in __call__
await await_many_dispatch([receive, self.channel_receive], self.dispatch)
File "/usr/local/lib/python3.6/dist-packages/channels/utils.py", line 50, in await_many_dispatch
await dispatch(result)
File "/usr/local/lib/python3.6/dist-packages/channels/consumer.py", line 67, in dispatch
await handler(message)
File "/usr/local/lib/python3.6/dist-packages/channels/generic/websocket.py", line 173, in websocket_connect
await self.connect()
File "/home/deploy/xxxxxx/app/consumers.py", line 314, in connect
await self.accept()
File "/usr/local/lib/python3.6/dist-packages/channels/generic/websocket.py", line 186, in accept
await super().send({"type": "websocket.accept", "subprotocol": subprotocol})
File "/usr/local/lib/python3.6/dist-packages/channels/consumer.py", line 75, in send
await self.base_send(message)
File "/usr/local/lib/python3.6/dist-packages/channels/sessions.py", line 226, in send
return await self.real_send(message)
File "/usr/local/lib/python3.6/dist-packages/uvicorn/protocols/websockets/websockets_impl.py", line 170, in asgi_send
self.scope["server"][0],
TypeError: 'NoneType' object is not subscriptable
Any ideas?
Django==2.1.3 channels==2.1.5 channels_redis==2.3.1 gunicorn==19.9.0 uvicorn==0.3.20
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 15 (11 by maintainers)
Commits related to this issue
- Fixes bug #242, Exception in ASGI application - 'NoneType' object is not subscriptable, server is not defined in scope — committed to agronick/uvicorn by agronick 6 years ago
- Revert "Fixes bug #242, Exception in ASGI application - 'NoneType' object is not subscriptable, server is not defined in scope" This reverts commit fdea7ff0463bb0b6bcad60497ffa08264fced4ea. — committed to agronick/uvicorn by agronick 5 years ago
- Only set text_data or bytes_data but not both (#349) * Fixes bug #242, Exception in ASGI application - 'NoneType' object is not subscriptable, server is not defined in scope * Only set text or byt... — committed to encode/uvicorn by agronick 5 years ago
Its really easy to see how this is happening.
get_local_addr(transport)returns None if the sockname key is not set. You just follow that code to where it gets set on the scope and its taking [0] from None. What should that function return when sockname is not set (it appears to never be set) instead of None?This issue is very easy to produce and very easy to fix. Happens out of the box with latest version and prevents websockets from working.
It comes from protocol/utils.py in this method
If you change it to
return ['', 0]the problem goes away. This method directly populates the scope’s server variable. As far as I can tell the variable sockname does not appear in anywhere in Uvicorn or Gunicorn.