channels_redis
channels_redis copied to clipboard
Crash in redis/asyncio/connection.py using Django Channels
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:
- Python version: 3.11.7
- Django version: 5.0.3
- Channels version: 4.1.0
- Channels Redis version: 4.2.0
- Redis version: 5.4.0
- Operating System: Ubuntu 20.04
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:
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.