{"id":"12663951e959bb20","repo":"redis/redis-py","slug":"command-command-name-not-found-in-commands","errorCode":null,"errorMessage":"Command {command_name} not found in commands","messagePattern":"Command (.+?) not found in commands","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"redis/_parsers/commands.py","lineNumber":253,"sourceCode":"        Raises:\n            ValueError\n                If the specified subcommand is not found within the command or the\n                specified command does not exist in the available commands.\n        \"\"\"\n        if subcommand_name:\n            for subcommand in self.commands.get(command_name)[\"subcommands\"]:\n                if str_if_bytes(subcommand[0]) == subcommand_name:\n                    parsed_subcmd = self.parse_subcommand(subcommand)\n                    return parsed_subcmd[\"first_key_pos\"] <= 0\n            raise ValueError(\n                f\"Subcommand {subcommand_name} not found in command {command_name}\"\n            )\n        else:\n            command_details = self.commands.get(command_name, None)\n            if command_details is not None:\n                return command_details[\"first_key_pos\"] <= 0\n\n            raise ValueError(f\"Command {command_name} not found in commands\")\n\n    def get_command_policies(self) -> PolicyRecords:\n        \"\"\"\n        Retrieve and process the command policies for all commands and subcommands.\n\n        This method traverses through commands and subcommands, extracting policy details\n        from associated data structures and constructing a dictionary of commands with their\n        associated policies. It supports nested data structures and handles both main commands\n        and their subcommands.\n\n        Returns:\n            PolicyRecords: A collection of commands and subcommands associated with their\n            respective policies.\n\n        Raises:\n            IncorrectPolicyType: If an invalid policy type is encountered during policy extraction.\n        \"\"\"\n        command_with_policies = {}","sourceCodeStart":235,"sourceCodeEnd":271,"githubUrl":"https://github.com/redis/redis-py/blob/da03cdc7e8731092b13e395605c3c1fb2de25de1/redis/_parsers/commands.py#L235-L271","documentation":"Raised by CommandsParser._is_keyless_command() when no subcommand is supplied but command_name is not a key in self.commands. This means the policy/keyless lookup is being run for a command the parser never received from COMMAND. Distinct from error 4 in that this fires during the keyless-policy computation loop rather than the routing get_keys() path.","triggerScenarios":"get_command_policies() iterating over self.commands and calling _is_keyless_command(command) for a name that was somehow removed from the dict concurrently; calling _is_keyless_command directly with a command name that COMMAND did not return; race where the cache was cleared mid-iteration.","commonSituations":"Concurrent reinitialize() clearing self.commands while another thread iterates; passing a stale command name captured before a reconnect; misbehaving module that mutates the parser's commands dict.","solutions":["Avoid concurrent calls that reinitialize() the parser while another caller is computing policies; serialize topology refresh.","Validate command_name in self.commands before invoking _is_keyless_command.","Upgrade redis-py - later versions guard against the concurrent-clear race.","Recreate the CommandsParser if the underlying connection was reset."],"exampleFix":"// before\nparser._is_keyless_command(\"mycmd\")  # ValueError if 'mycmd' missing\n\n// after\nif \"mycmd\" not in parser.commands:\n    parser.initialize(redis_conn)\nassert \"mycmd\" in parser.commands\nparser._is_keyless_command(\"mycmd\")","handlingStrategy":"validation","validationCode":"# Validate the command is in the cache before the keyless lookup\ndef assert_command_known(parser, name):\n    if name not in parser.commands:\n        parser.initialize(parser.redis_connection)\n    if name not in parser.commands:\n        raise KeyError(f\"command '{name}' not in COMMAND output\")","typeGuard":null,"tryCatchPattern":"try:\n    parser._is_keyless_command(name)\nexcept ValueError as e:\n    if \"not found in commands\" in str(e):\n        parser.initialize(parser.redis_connection)  # refresh then retry once\n        parser._is_keyless_command(name)","preventionTips":["Serialize topology/policy refresh so initialize() does not race with iteration.","Validate command names against parser.commands before policy lookups.","Recreate the CommandsParser after a cluster reset instead of reusing a stale one.","Upgrade redis-py to pick up race-condition guards."],"tags":["cluster","commands","validation","sync"],"analyzedSha":"da03cdc7e8731092b13e395605c3c1fb2de25de1","analyzedAt":"2026-08-04T20:26:47.563Z","schemaVersion":2}