redis/redis-py · error · ConnectionError

Error setting client name

Error message

Error setting client name

What it means

Raised as a ConnectionError during the connection handshake (on_connect) when the CLIENT SETNAME command is sent (because self.client_name is set) but the server returns a value that is not the string 'OK'. This means the server did not acknowledge the name assignment, usually because the name violates server naming rules (e.g. contains spaces/newlines) or the server is a proxy/compatibility layer that does not implement CLIENT SETNAME correctly.

Source

Thrown at redis/asyncio/connection.py:1025

            #     raise ConnectionError("Invalid RESP version")

        # Activate maintenance notifications for this connection
        # if enabled in the configuration
        # This is a no-op if maintenance notifications are not enabled
        await self.activate_maint_notifications_handling_if_enabled(
            check_health=check_health
        )

        # if a client_name is given, set it
        if self.client_name:
            await self.send_command(
                "CLIENT",
                "SETNAME",
                self.client_name,
                check_health=check_health,
            )
            if str_if_bytes(await self.read_response()) != "OK":
                raise ConnectionError("Error setting client name")

        # Set the library name and version from driver_info, pipeline for lower startup latency
        lib_name_sent = False
        lib_version_sent = False

        if self.driver_info and self.driver_info.formatted_name:
            await self.send_command(
                "CLIENT",
                "SETINFO",
                "LIB-NAME",
                self.driver_info.formatted_name,
                check_health=check_health,
            )
            lib_name_sent = True

        if self.driver_info and self.driver_info.lib_version:
            await self.send_command(
                "CLIENT",

View on GitHub (pinned to da03cdc7e8)

Solutions

  1. Sanitize the client_name to contain only printable characters with no spaces or newlines.
  2. Verify the target actually speaks the Redis protocol and supports CLIENT SETNAME (point a raw redis-cli at it and run 'CLIENT SETNAME test').
  3. Temporarily drop the client_name argument to confirm it is the cause; re-add a clean value once the connection succeeds.
  4. Upgrade or reconfigure the proxy/load balancer to pass CLIENT commands through unchanged.

Example fix

// before
r = redis.asyncio.Redis(host=h, port=p, client_name="my worker #3")

// after
r = redis.asyncio.Redis(host=h, port=p, client_name="my-worker-3")
Defensive patterns

Strategy: validation

Validate before calling

import re
NAME_RE = re.compile(r"^[\x21-\x7e]+$")  # printable, no spaces/newlines
def valid_client_name(name: str | None) -> bool:
    return name is None or (NAME_RE.match(name) is not None and len(name) <= 200)

Type guard

def is_valid_client_name(name) -> bool:
    return name is None or (isinstance(name, str) and len(name) <= 200 and " " not in name and "\n" not in name)

Try / catch

from redis.exceptions import ConnectionError
try:
    await r.ping()
except ConnectionError as e:
    if "setting client name" in str(e):
        r = redis.asyncio.Redis(..., client_name=None)  # retry without name
    else:
        raise

Prevention

When it happens

Trigger: Constructing redis.asyncio.Redis(client_name='...') (or connection_pool with client_name) and opening a connection whose on_connect() issues 'CLIENT SETNAME <name>'; the read_response() for that command returns something other than b'OK'/'OK'. Only fires when self.client_name is truthy at line 1017.

Common situations: Client names containing spaces, newlines, or non-printable characters (Redis disallows these); connecting to a Redis-compatible proxy (KeyDB, Dragonfly, Valkey, Twemproxy, a load balancer) that rejects or mishandles CLIENT SETNAME; older server versions where the name was too long or already in use.

Related errors


AI-assisted analysis of redis/redis-py@da03cdc7e8 (2026-08-04). Data as JSON: /data/errors/d4e51ea90cb9fee2.json. Report an issue: GitHub.