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

Exception in ASGI application with uvicorn #1959

Closed
ghost opened this issue Dec 4, 2022 · 2 comments
Closed

Exception in ASGI application with uvicorn #1959

ghost opened this issue Dec 4, 2022 · 2 comments

Comments

@ghost
Copy link

ghost commented Dec 4, 2022

Problem when we use channels v3, after some time in production, connection to establish websockets start to work more and more slowly, sometimes it take 1-3 seconds to accept websockets connection and with more time on prod time increases to 170 sec and more, and at the end nginx kill connections (in our setup it`s 300 sec) and you see 502 timeouts.

when we use --limit-max-requests=1000 and --proxy-headers we see errors [Exception in ASGI application],
but after we remove --limit-max-requests=1000 and --proxy-headers all works as expected (only in channels v3 in v4 nothing is working).

Django==4.0.6
daphne==3.0.2
asgiref==3.5.2
channels==3.0.5
channels-redis==3.3.0
websockets==10.4
uvicorn==0.19.0
uvloop==0.17.0
=========================
| ERROR:    Exception in ASGI application
| Traceback (most recent call last):
|   File "/usr/local/lib/python3.10/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 230, in run_asgi
|     result = await self.app(self.scope, self.asgi_receive, self.asgi_send)
|   File "/usr/local/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
|     return await self.app(scope, receive, send)
|   File "/usr/local/lib/python3.10/site-packages/channels/routing.py", line 71, in __call__
|     return await application(scope, receive, send)
|   File "/usr/local/lib/python3.10/site-packages/channels/routing.py", line 150, in __call__
|     return await application(
|   File "/usr/local/lib/python3.10/site-packages/channels/consumer.py", line 94, in app
|     return await consumer(scope, receive, send)
|   File "/usr/local/lib/python3.10/site-packages/channels/consumer.py", line 58, in __call__
|     await await_many_dispatch(
|   File "/usr/local/lib/python3.10/site-packages/channels/utils.py", line 58, in await_many_dispatch
|     await task
| TypeError: An asyncio.Future, a coroutine or an awaitable is required

=========================
## uvicorn
uvicorn --host=0.0.0.0 --port=8002 --workers=4 --loop=uvloop --ws=websockets --limit-max-requests=1000 --proxy-headers --log-level=debug core.asgi:application

# simple test class that produce this error

```python
class TestWebSocket(AsyncJsonWebsocketConsumer):
    async def connect(self):
        try:
            await self.create_channel()
            await self.accept()
       except Exception as exc:
            logger.exception(exc)
            await self.close()

    @database_sync_to_async
    def create_channel(self):
         # some logic with database.
        return
Django==4.1.3
daphne==4.0.0
asgiref==3.5.2
channels==4.0.0
channels-redis==4.0.0
websockets==10.4
uvicorn==0.20.0
uvloop==0.17.0
=========================
| ERROR:    Exception in ASGI application
| Traceback (most recent call last):
|   File "/usr/local/lib/python3.10/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 230, in run_asgi
|     result = await self.app(self.scope, self.asgi_receive, self.asgi_send)
|   File "/usr/local/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
|     return await self.app(scope, receive, send)
|   File "/usr/local/lib/python3.10/site-packages/channels/routing.py", line 71, in __call__
|     return await application(scope, receive, send)
|   File "/usr/local/lib/python3.10/site-packages/channels/routing.py", line 150, in __call__
|     return await application(
|   File "/usr/local/lib/python3.10/site-packages/channels/consumer.py", line 94, in app
|     return await consumer(scope, receive, send)
|   File "/usr/local/lib/python3.10/site-packages/channels/consumer.py", line 58, in __call__
|     await await_many_dispatch(
|   File "/usr/local/lib/python3.10/site-packages/channels/utils.py", line 58, in await_many_dispatch
|     await task
| AttributeError: 'list' object has no attribute 'decode'
=========================
uvicorn --host=0.0.0.0 --port=8002 --workers=4 --loop=uvloop --ws=websockets --limit-max-requests=1000 --proxy-headers --log-level=debug core.asgi:application
@ghost
Copy link
Author

ghost commented Jan 31, 2023

Problem was in CHANNEL_LAYERS config, for some reason old version 3 works with list setup example: "hosts": [["127.0.0.1", 6379]],
and migrating to v4 start raise "AttributeError: 'list' object has no attribute 'decode'".

it is very difficult to find and identify such a small mistake.

@carltongibson
Copy link
Member

Duplicate of django/channels_redis#334

@carltongibson carltongibson marked this as a duplicate of django/channels_redis#334 Apr 30, 2023
@carltongibson carltongibson closed this as not planned Won't fix, can't repro, duplicate, stale Apr 30, 2023
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

No branches or pull requests

1 participant