Skip to content

Unable to connect to redis sentinel. Getting: unexpected keyword argument 'connection_pool' #3595

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

Open
Manhar0911 opened this issue Apr 11, 2025 · 5 comments
Assignees

Comments

@Manhar0911
Copy link

Manhar0911 commented Apr 11, 2025

I am using python client to connect with sentinel master. But getting this error

Traceback (most recent call last): File "/usr/local/lib/python3.12/site-packages/ecprt/agents/cli/cli.py", line 266, in _worker_run await worker.run() File "/usr/local/lib/python3.12/site-packages/ecprt/agents/worker.py", line 550, in run self.connect_to_redis() File "/usr/local/lib/python3.12/site-packages/ecprt/agents/worker.py", line 476, in connect_to_redis redis_client.ping() File "/usr/local/lib/python3.12/site-packages/redis/commands/core.py", line 1212, in ping return self.execute_command("PING", **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/redis/client.py", line 559, in execute_command return self._execute_command(*args, **options) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/redis/client.py", line 565, in _execute_command conn = self.connection or pool.get_connection(command_name, **options) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/redis/connection.py", line 1417, in get_connection connection = self.make_connection() ^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/redis/connection.py", line 1463, in make_connection return self.connection_class(**self.connection_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/redis/connection.py", line 1013, in __init__ super().__init__(**kwargs) File "/usr/local/lib/python3.12/site-packages/redis/connection.py", line 684, in __init__ super().__init__(**kwargs) TypeError: AbstractConnection.__init__() got an unexpected keyword argument 'connection_pool' {"message": "worker failed", "level": "ERROR", "name": "livekit.agents", "exc_info": "Traceback (most recent call last):\n File \"/usr/local/lib/python3.12/site-packages/redis/connection.py\", line 1415, in get_connection\n connection = self._available_connections.pop()\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\nIndexError: pop from empty list\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n File \"/usr/local/lib/python3.12/site-packages/myapp/cli/cli.py\", line 266, in _worker_run\n await worker.run()\n File \"/usr/local/lib/python3.12/site-packages/myapp/worker.py\", line 550, in run\n self.connect_to_redis()\n File \"/usr/local/lib/python3.12/site-packages/myapp/worker.py\", line 476, in connect_to_redis\n redis_client.ping()\n File \"/usr/local/lib/python3.12/site-packages/redis/commands/core.py\", line 1212, in ping\n return self.execute_command(\"PING\", **kwargs)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/usr/local/lib/python3.12/site-packages/redis/client.py\", line 559, in execute_command\n return self._execute_command(*args, **options)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/usr/local/lib/python3.12/site-packages/redis/client.py\", line 565, in _execute_command\n conn = self.connection or pool.get_connection(command_name, **options)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/usr/local/lib/python3.12/site-packages/redis/connection.py\", line 1417, in get_connection\n connection = self.make_connection()\n ^^^^^^^^^^^^^^^^^^^^^^\n File \"/usr/local/lib/python3.12/site-packages/redis/connection.py\", line 1463, in make_connection\n return self.connection_class(**self.connection_kwargs)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/usr/local/lib/python3.12/site-packages/redis/connection.py\", line 1013, in __init__\n super().__init__(**kwargs)\n File \"/usr/local/lib/python3.12/site-packages/redis/connection.py\", line 684, in __init__\n super().__init__(**kwargs)\nTypeError: AbstractConnection.__init__() got an unexpected keyword argument 'connection_pool'", "timestamp": "2025-04-11T08:47:09.997584+00:00"}

Below is my code for connecting with sentinel master node.

`def connect_to_redis(self):

logger.info(f"Redis version in use: {redis.__version__}")
    logger.info("Inside connect_to_redis")

    use_tls = os.getenv("REDIS_USE_TLS", "true").lower() == "true"
    cacert_path = os.getenv("REDIS_CACERT_PATH", "/etc/rediscert/ca.crt")

    sentinel_addresses_raw = os.getenv("REDIS_SENTINEL_ADDRESSES", "sentinel.redis-ptdev5-ns.svc.cluster.local:26379")
    sentinel_addresses = sentinel_addresses_raw.strip("[]").split(",")
    sentinel_master_name = os.getenv("REDIS_SENTINEL_MASTER_NAME", "master")

    dial_timeout = int(os.getenv("REDIS_DIAL_TIMEOUT", "2000")) / 1000
    read_timeout = int(os.getenv("REDIS_READ_TIMEOUT", "200")) / 1000
    write_timeout = int(os.getenv("REDIS_WRITE_TIMEOUT", "200")) / 1000

    if not (sentinel_addresses and sentinel_addresses[0]):
        raise Exception("Redis is not configured - REDIS_SENTINEL_ADDRESSES required")

    sentinel_addrs = []
    for addr in sentinel_addresses:
        addr = addr.strip()
        if addr:
            if ':' in addr:
                host, port_str = addr.rsplit(':', 1)
                port = int(port_str.strip("]'\""))
            else:
                host = addr
                port = 26379
            sentinel_addrs.append((host, port))

    try:
        connection_class = redis.SSLConnection if use_tls else redis.Connection

        sentinel = Sentinel(
            sentinel_addrs,
            socket_timeout=read_timeout,
            connection_class=connection_class,
            ssl=use_tls if use_tls else None,
            ssl_ca_certs=cacert_path if use_tls else None,
        )

        global redis_client
        redis_client = sentinel.master_for(
            service_name=sentinel_master_name,
            socket_timeout=read_timeout,
        )

        try:
            redis_client.ping()
            logger.info("Successfully connected to Redis")
        except redis.RedisError as e:
            logger.warning(f"Redis client initialized, but ping failed: {e}")
            pass

    except Exception as e:
        logger.error(f"Redis sentinel connection setup failed: {e}")
        raise`
@petyaslavova petyaslavova self-assigned this Apr 14, 2025
@petyaslavova
Copy link
Collaborator

Hi @Manhar0911, thank you for bringing this to our attention. I've identified the root cause and will be posting a fix shortly.

@petyaslavova
Copy link
Collaborator

@Manhar0911 Actually, when digging a bit deeper into the code, I noticed that a different class needs to be used for Sentinel connections. In your example, you provided SSLContext, but for Sentinel connections, SentinelManagedSSLConnection should be used instead. Moreover, when ssl=True is specified for Sentinel, there's no need to explicitly set the connection class — it's automatically mapped.

@Manhar0911
Copy link
Author

@petyaslavova can you please give a pseudo code for above. Also specify which redis version to use.
I updated the code like this but after connecting with redis, ping fails with error

Redis client initialized, but ping failed: No master found for 'master'
my sentinel master name is master only.

sentinel = redis.sentinel.Sentinel(
    sentinel_addrs,
    sentinel_kwargs={'socket_connect_timeout': 5.0, 'socket_timeout': 5.0},
    connection_class=SSLSentinelManagedConnection,
    ssl_ca_certs=cacert_path,
)

redis_client = sentinel.master_for(
    service_name=sentinel_master_name,
    socket_timeout=read_timeout,
)

try:
    redis_client.ping()
    logger.info("Successfully connected to Redis")
except redis.RedisError as e:
    logger.warning(f"Redis client initialized, but ping failed: {e}")

@petyaslavova
Copy link
Collaborator

Hi @Manhar0911, I hit another issue after setting up the correct class, and there is already an existing old (still open) issue about it. It will need some time to be done properly. I'll add this to my todo list.

@petyaslavova
Copy link
Collaborator

This issue appears to be a duplicate of #3128

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

2 participants