{"record":{"id":"ea4d5972a3a378a6","repo":"redis/redis-py","slug":"error-setting-client-name-ea4d59","errorCode":null,"errorMessage":"Error setting client name","messagePattern":"Error setting client name","errorType":"exception","errorClass":"ConnectionError","httpStatus":null,"severity":"error","filePath":"redis/connection.py","lineNumber":1207,"sourceCode":"                and self.handshake_metadata.get(\"proto\") != self.protocol\n            ):\n                raise ConnectionError(\"Invalid RESP version\")\n\n        # Activate maintenance notifications for this connection\n        # if enabled in the configuration\n        # This is a no-op if maintenance notifications are not enabled\n        self.activate_maint_notifications_handling_if_enabled(check_health=check_health)\n\n        # if a client_name is given, set it\n        if self.client_name:\n            self.send_command(\n                \"CLIENT\",\n                \"SETNAME\",\n                self.client_name,\n                check_health=check_health,\n            )\n            if str_if_bytes(self.read_response()) != \"OK\":\n                raise ConnectionError(\"Error setting client name\")\n\n        # Set the library name and version from driver_info\n        try:\n            if self.driver_info and self.driver_info.formatted_name:\n                self.send_command(\n                    \"CLIENT\",\n                    \"SETINFO\",\n                    \"LIB-NAME\",\n                    self.driver_info.formatted_name,\n                    check_health=check_health,\n                )\n                self.read_response()\n        except ResponseError:\n            pass\n\n        try:\n            if self.driver_info and self.driver_info.lib_version:\n                self.send_command(","sourceCodeStart":1189,"sourceCodeEnd":1225,"githubUrl":"https://github.com/redis/redis-py/blob/6a6b581b48225afa0b76912d1028c6035baee932/redis/connection.py#L1189-L1225","documentation":"Raised in on_connect (connection.py:1199-1207) when client_name is set and the CLIENT SETNAME reply is not 'OK'. The library sets the client name right after connect/auth; if the server rejects SETNAME (name too long, contains invalid chars, or non-OK for any reason) the connection is aborted. This protects against silently running unnamed connections.","triggerScenarios":"Passing client_name with characters Redis disallows, or longer than the server's limit (CONFIG set max name length, historically 255 and must not contain spaces/newlines in older versions), when connecting.","commonSituations":"Embedding spaces or newlines in client_name; generating very long dynamic names (e.g. a full hostname + uuid); connecting to an old Redis with stricter name rules.","solutions":["Shorten client_name and remove spaces/newlines (use hyphens or underscores).","Confirm the server's client-name rules (CLIENT SETNAME docs) for your Redis version.","Omit client_name if it is not required.","Use ASCII alphanumeric names with separators."],"exampleFix":"# before\nr = redis.Redis(host=h, port=p, client_name='my service (prod) #1')\n# after\nr = redis.Redis(host=h, port=p, client_name='my-service-prod-1')","handlingStrategy":"validation","validationCode":"import re\ndef valid_client_name(name: str) -> bool:\n    # Redis CLIENT SETNAME: no spaces/newlines; reasonable length\n    return bool(name) and len(name) <= 255 and re.fullmatch(r'[^\\s\\r\\n]+', name)\n\nname = client_name if valid_client_name(client_name) else None\nr = redis.Redis(host=h, port=p, client_name=name)","typeGuard":"import re\ndef is_valid_client_name(name) -> bool:\n    return isinstance(name, str) and len(name) <= 255 and re.fullmatch(r'[^\\s\\r\\n]+', name)","tryCatchPattern":"from redis.exceptions import ConnectionError\nimport re\ntry:\n    r = redis.Redis(host=h, port=p, client_name=name)\n    r.ping()\nexcept ConnectionError as e:\n    if 'client name' in str(e):\n        safe = re.sub(r'\\s+', '-', name)[:64]\n        r = redis.Redis(host=h, port=p, client_name=safe)\n    else:\n        raise","preventionTips":["Generate client names from a fixed charset ([A-Za-z0-9_-]).","Cap generated names to ~64 chars to stay well under limits.","Validate dynamic names before passing them as client_name."],"tags":["client-name","configuration","connection","validation"],"backgroundTag":null,"analyzedSha":"6a6b581b48225afa0b76912d1028c6035baee932","analyzedAt":"2026-08-10T12:52:44.840Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}