Hosted Django app on render connecting to azure redis for channel layer but getting error

Traceback:

2023-08-22 12:38:41,950 INFO     Listening on TCP address 0.0.0.0:8000
Aug 22 03:38:47 PM  2023-08-22 12:38:47,850 ERROR    Exception inside application: Error connecting to localhost:6379. Multiple exceptions: [Errno 111] Connect call failed ('::1', 6379, 0, 0), [Errno 111] Connect call failed ('127.0.0.1', 6379).
Aug 22 03:38:47 PM  Traceback (most recent call last):
Aug 22 03:38:47 PM    File "/opt/render/project/src/.venv/lib/python3.10/site-packages/redis/asyncio/connection.py", line 243, in connect
Aug 22 03:38:47 PM      await self.retry.call_with_retry(
Aug 22 03:38:47 PM    File "/opt/render/project/src/.venv/lib/python3.10/site-packages/redis/asyncio/retry.py", line 59, in call_with_retry
Aug 22 03:38:47 PM      return await do()
Aug 22 03:38:47 PM    File "/opt/render/project/src/.venv/lib/python3.10/site-packages/redis/asyncio/connection.py", line 649, in _connect
Aug 22 03:38:47 PM      reader, writer = await asyncio.open_connection(
Aug 22 03:38:47 PM    File "/opt/render/project/python/Python-3.10.11/lib/python3.10/asyncio/streams.py", line 48, in open_connection
Aug 22 03:38:47 PM      transport, _ = await loop.create_connection(
Aug 22 03:38:47 PM    File "/opt/render/project/python/Python-3.10.11/lib/python3.10/asyncio/base_events.py", line 1084, in create_connection
Aug 22 03:38:47 PM      raise OSError('Multiple exceptions: {}'.format(
Aug 22 03:38:47 PM  OSError: Multiple exceptions: [Errno 111] Connect call failed ('::1', 6379, 0, 0), [Errno 111] Connect call failed ('127.0.0.1', 6379)
Aug 22 03:38:47 PM  
Aug 22 03:38:47 PM  During handling of the above exception, another exception occurred:
Aug 22 03:38:47 PM  
Aug 22 03:38:47 PM  Traceback (most recent call last):
Aug 22 03:38:47 PM    File "/opt/render/project/src/.venv/lib/python3.10/site-packages/channels/utils.py", line 50, in await_many_dispatch
Aug 22 03:38:47 PM      await dispatch(result)
Aug 22 03:38:47 PM    File "/opt/render/project/src/.venv/lib/python3.10/site-packages/asgiref/sync.py", line 448, in __call__
Aug 22 03:38:47 PM      ret = await asyncio.wait_for(future, timeout=None)
Aug 22 03:38:47 PM    File "/opt/render/project/python/Python-3.10.11/lib/python3.10/asyncio/tasks.py", line 408, in wait_for
Aug 22 03:38:47 PM      return await fut
Aug 22 03:38:47 PM    File "/opt/render/project/python/Python-3.10.11/lib/python3.10/concurrent/futures/thread.py", line 58, in run
Aug 22 03:38:47 PM      result = self.fn(*self.args, **self.kwargs)
Aug 22 03:38:47 PM    File "/opt/render/project/src/.venv/lib/python3.10/site-packages/channels/db.py", line 13, in thread_handler
Aug 22 03:38:47 PM      return super().thread_handler(loop, *args, **kwargs)
Aug 22 03:38:47 PM    File "/opt/render/project/src/.venv/lib/python3.10/site-packages/asgiref/sync.py", line 490, in thread_handler
Aug 22 03:38:47 PM      return func(*args, **kwargs)
Aug 22 03:38:47 PM    File "/opt/render/project/src/.venv/lib/python3.10/site-packages/channels/consumer.py", line 125, in dispatch
Aug 22 03:38:47 PM      handler(message)
Aug 22 03:38:47 PM    File "/opt/render/project/src/.venv/lib/python3.10/site-packages/channels/generic/websocket.py", line 38, in websocket_connect
Aug 22 03:38:47 PM      self.connect()
Aug 22 03:38:47 PM    File "/opt/render/project/src/./events/consumers.py", line 89, in connect
Aug 22 03:38:47 PM      async_to_sync(self.channel_layer.group_add)(self.group_name, self.channel_name)
Aug 22 03:38:47 PM    File "/opt/render/project/src/.venv/lib/python3.10/site-packages/asgiref/sync.py", line 240, in __call__
Aug 22 03:38:47 PM      return call_result.result()
Aug 22 03:38:47 PM    File "/opt/render/project/python/Python-3.10.11/lib/python3.10/concurrent/futures/_base.py", line 451, in result
Aug 22 03:38:47 PM      return self.__get_result()
Aug 22 03:38:47 PM    File "/opt/render/project/python/Python-3.10.11/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result
Aug 22 03:38:47 PM      raise self._exception
Aug 22 03:38:47 PM    File "/opt/render/project/src/.venv/lib/python3.10/site-packages/asgiref/sync.py", line 306, in main_wrap
Aug 22 03:38:47 PM      result = await self.awaitable(*args, **kwargs)
Aug 22 03:38:47 PM    File "/opt/render/project/src/.venv/lib/python3.10/site-packages/channels_redis/core.py", line 498, in group_add
Aug 22 03:38:47 PM      await connection.zadd(group_key, {channel: time.time()})
Aug 22 03:38:47 PM    File "/opt/render/project/src/.venv/lib/python3.10/site-packages/redis/asyncio/client.py", line 545, in execute_command
Aug 22 03:38:47 PM      conn = self.connection or await pool.get_connection(command_name, **options)
Aug 22 03:38:47 PM    File "/opt/render/project/src/.venv/lib/python3.10/site-packages/redis/asyncio/connection.py", line 1105, in get_connection
Aug 22 03:38:47 PM      await connection.connect()
Aug 22 03:38:47 PM    File "/opt/render/project/src/.venv/lib/python3.10/site-packages/redis/asyncio/connection.py", line 251, in connect
Aug 22 03:38:47 PM      raise ConnectionError(self._error_message(e))
Aug 22 03:38:47 PM  redis.exceptions.ConnectionError: Error connecting to localhost:6379. Multiple exceptions: [Errno 111] Connect call failed ('::1', 6379, 0, 0), [Errno 111] Connect call failed ('127.0.0.1', 6379).
Aug 22 03:38:47 PM  
Aug 22 03:38:47 PM  During handling of the above exception, another exception occurred:
Aug 22 03:38:47 PM  
Aug 22 03:38:47 PM  Traceback (most recent call last):
Aug 22 03:38:47 PM    File "/opt/render/project/src/.venv/lib/python3.10/site-packages/channels/routing.py", line 62, in __call__
Aug 22 03:38:47 PM      return await application(scope, receive, send)
Aug 22 03:38:47 PM    File "/opt/render/project/src/.venv/lib/python3.10/site-packages/channels/routing.py", line 116, in __call__
Aug 22 03:38:47 PM      return await application(
Aug 22 03:38:47 PM    File "/opt/render/project/src/.venv/lib/python3.10/site-packages/channels/consumer.py", line 94, in app
Aug 22 03:38:47 PM      return await consumer(scope, receive, send)
Aug 22 03:38:47 PM    File "/opt/render/project/src/.venv/lib/python3.10/site-packages/channels/consumer.py", line 58, in __call__
Aug 22 03:38:47 PM      await await_many_dispatch(
Aug 22 03:38:47 PM    File "/opt/render/project/src/.venv/lib/python3.10/site-packages/channels/utils.py", line 57, in await_many_dispatch
Aug 22 03:38:47 PM      await task
Aug 22 03:38:47 PM    File "/opt/render/project/src/.venv/lib/python3.10/site-packages/channels_redis/core.py", line 347, in receive
Aug 22 03:38:47 PM      message_channel, message = await self.receive_single(
Aug 22 03:38:47 PM    File "/opt/render/project/src/.venv/lib/python3.10/site-packages/channels_redis/core.py", line 402, in receive_single
Aug 22 03:38:47 PM      content = await self._brpop_with_clean(
Aug 22 03:38:47 PM    File "/opt/render/project/src/.venv/lib/python3.10/site-packages/channels_redis/core.py", line 238, in _brpop_with_clean
Aug 22 03:38:47 PM      await connection.eval(cleanup_script, 0, channel, backup_queue)
Aug 22 03:38:47 PM    File "/opt/render/project/src/.venv/lib/python3.10/site-packages/redis/asyncio/client.py", line 545, in execute_command
Aug 22 03:38:47 PM      conn = self.connection or await pool.get_connection(command_name, **options)
Aug 22 03:38:47 PM    File "/opt/render/project/src/.venv/lib/python3.10/site-packages/redis/asyncio/connection.py", line 1105, in get_connection
Aug 22 03:38:47 PM      await connection.connect()
Aug 22 03:38:47 PM    File "/opt/render/project/src/.venv/lib/python3.10/site-packages/redis/asyncio/connection.py", line 251, in connect
Aug 22 03:38:47 PM      raise ConnectionError(self._error_message(e))
Aug 22 03:38:47 PM  redis.exceptions.ConnectionError: Error connecting to localhost:6379. Multiple exceptions: [Errno 111] Connect call failed ('::1', 6379, 0, 0), [Errno 111] Connect call failed ('127.0.0.1', 6379).
Aug 22 03:38:48 PM  ==> Detected service running on port 8000
Aug 22 03:38:48 PM  ==> Docs on specifying a port: https://render.com/docs/web-services#port-detection
Aug 22 03:38:51 PM  Your service is live 🎉
Aug 22 03:38:51 PM  Not Found: /
Aug 22 03:38:51 PM  2023-08-22 12:38:51,743 WARNING  Not Found: /
Aug 22 03:39:51 PM  2023-08-22 12:39:51,349 INFO     Killed 0 pending application instances
Aug 22 03:39:51 PM  10.204.220.104:58458 - - [22/Aug/2023:12:38:46] "WSCONNECTING /ws/images/event/5ae78844-1a3a-4f05-9022-d498df33414d" - -
Aug 22 03:39:51 PM  10.204.220.104:58458 - - [22/Aug/2023:12:38:47] "WSDISCONNECT /ws/images/event/5ae78844-1a3a-4f05-9022-d498df33414d" - -

my settings.py

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "host": [os.environ.get('REDIS_URL')]
    },
}

REDIS_URL is enviroment variable set with link to my redis instance

i have even tried using the redis provided by renser but dont seem to know why it doesnt work on render but works locally.

Hey there,

This line:

[Errno 111] Connect call failed (‘127.0.0.1’, 6379).

In the Stacktrace you provided shows your application is attempting to connect to Redis on localhost and not an Azure - worth checking Redis URL is what you think and that you’re using it correctly here,

Regards,

John B
Render Support, UTC+1 :uk:

thanks very much . as you pointed out django was getting localhost which is the default. i made a mistake in my config it its supposed to be

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "host": [os.getenv('REDIS_URL')]
    },
}

not

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "host": [os.environ.get('REDIS_URL')]
    },
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.