django / channels_redis

Redis channel layer backend for Django Channels
BSD 3-Clause "New" or "Revised" License
582 stars 197 forks source link

Crash in redis/asyncio/connection.py using Django Channels #395

Open developcreativo opened 1 month ago

developcreativo commented 1 month ago

I'm encountering an issue when using Redis with Django Channels, where the connection crashes. Below are the details of my setup and an example of the code that's causing the issue:

Environment:

Code Example:

from channels.generic.websocket import AsyncWebsocketConsumer

class ChatConsumer(AsyncWebsocketConsumer):
    channel_layer_alias = "chat"

    async def connect(self):
        user = self.scope["user"]
        if user.is_authenticated:
            # Accept the connection
            await self.accept()
            # Add the channel to a group with the current user's id
            await self.channel_layer.group_add(str(user.id), self.channel_name)

            on_websocket_connect_task.apply_async(
                kwargs=dict(channel_name=self.channel_name, user_id=str(user.id)),
                queue=settings.QUEUE_CHAT,
            )
        else:
            self.close(code=1000)

Error: The connection crashes in redis/asyncio/connection.py. Here is the traceback:

image

This error occurs repeatedly when the socket remains inactive for hours. It happens not only in the chat application but also in others. To connect to a socket, we have to attempt to connect up to 3 times.

bigfootjon commented 1 month ago

I don't see a traceback (just an error message), do you have any further information about where the exception is being raised?

Do you have a minimal repro? I.e. do you have some self-contained code that I could run and see this problem?