{"record":{"id":"9a755d5ab412d55e","repo":"redis/redis-py","slug":"max-connections-must-be-a-positive-integer-9a755d","errorCode":null,"errorMessage":"\"max_connections\" must be a positive integer","messagePattern":"\"max_connections\" must be a positive integer","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"redis/connection.py","lineNumber":2993,"sourceCode":"        url_options = parse_url(url)\n\n        if \"connection_class\" in kwargs:\n            url_options[\"connection_class\"] = kwargs[\"connection_class\"]\n\n        kwargs.update(url_options)\n        return cls(**kwargs)\n\n    def __init__(\n        self,\n        connection_class=Connection,\n        max_connections: Optional[int] = None,\n        cache_factory: Optional[CacheFactoryInterface] = None,\n        maint_notifications_config: Optional[MaintNotificationsConfig] = None,\n        **connection_kwargs,\n    ):\n        max_connections = max_connections or 100\n        if not isinstance(max_connections, int) or max_connections < 0:\n            raise ValueError('\"max_connections\" must be a positive integer')\n\n        self.connection_class = connection_class\n        self._connection_kwargs = connection_kwargs\n        self.max_connections = max_connections\n        self.cache = None\n        self._cache_factory = cache_factory\n\n        try:\n            supports_maint_notifications = issubclass(\n                connection_class, MaintNotificationsAbstractConnection\n            )\n            is_unix_domain_socket_connection = issubclass(\n                connection_class, UnixDomainSocketConnection\n            )\n        except TypeError:\n            supports_maint_notifications = False\n            is_unix_domain_socket_connection = False\n","sourceCodeStart":2975,"sourceCodeEnd":3011,"githubUrl":"https://github.com/redis/redis-py/blob/6a6b581b48225afa0b76912d1028c6035baee932/redis/connection.py#L2975-L3011","documentation":"Raised as ValueError by ConnectionPool.__init__ when max_connections is not an int or is negative. Note the line `max_connections = max_connections or 100` first substitutes 100 for falsy values (None, 0, ''), so a 0 becomes 100 and will not raise; only a genuinely negative integer or a non-int (float, str) trips the check. The cap keeps the bounded pool from over-allocating.","triggerScenarios":"Passing max_connections=-1, max_connections=1.5, or max_connections='10' (string) to ConnectionPool. Pulling max_connections from config without coercing to int.","commonSituations":"Config value loaded as a string (e.g. os.environ returns str) and passed straight through. Negative values used to mean 'unlimited' by mistake. Float arithmetic producing 99.0.","solutions":["Pass a positive int: max_connections=100. To get the default, omit the argument or pass None.","Coerce config-sourced values: max_connections=int(value) and assert it is >= 0.","Remember 0 means 'use default (100)', not 'unlimited' — redis-py has no unlimited option."],"exampleFix":"# before\npool = ConnectionPool(max_connections=os.environ['MAX_CONN'])  # str -> raises\n# after\npool = ConnectionPool(max_connections=int(os.environ['MAX_CONN']))","handlingStrategy":"validation","validationCode":"def coerce_max_connections(value):\n    value = int(value)  # raise early on non-numeric strings\n    if value < 0:\n        raise ValueError('max_connections must be >= 0')\n    return value or 100  # 0 -> default\n\npool = ConnectionPool(max_connections=coerce_max_connections(cfg.get('max_connections')))","typeGuard":"def is_valid_max_connections(value) -> bool:\n    return isinstance(value, int) and not isinstance(value, bool) and value >= 0","tryCatchPattern":"try:\n    pool = ConnectionPool(max_connections=raw)\nexcept ValueError:\n    pool = ConnectionPool(max_connections=int(raw))","preventionTips":["Always coerce config/env values to int before passing as max_connections.","Remember 0 means default (100), not unlimited.","Reject negative values in config validation."],"tags":["configuration","connection-pool","validation"],"backgroundTag":null,"analyzedSha":"6a6b581b48225afa0b76912d1028c6035baee932","analyzedAt":"2026-08-10T12:52:44.840Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}