{"record":{"id":"866cff0b279d0bd1","repo":"redis/redis-py","slug":"max-connections-must-be-a-positive-integer","errorCode":null,"errorMessage":"\"max_connections\" must be a positive integer","messagePattern":"\"max_connections\" must be a positive integer","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"redis/asyncio/connection.py","lineNumber":2630,"sourceCode":"        ``ValueError`` to be raised. Once parsed, the querystring arguments\n        and keyword arguments are passed to the ``ConnectionPool``'s\n        class initializer. In the case of conflicting arguments, querystring\n        arguments always win.\n        \"\"\"\n        url_options = parse_url(url)\n        kwargs.update(url_options)\n        return cls(**kwargs)\n\n    def __init__(\n        self,\n        connection_class: Type[AbstractConnection] = Connection,\n        max_connections: Optional[int] = None,\n        maint_notifications_config: MaintNotificationsConfig | None = 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\n        # Resolve the HIMPORT registry. A pre-built ``himport_registry`` (shared, e.g.\n        # from the cluster client) takes precedence; otherwise build a fresh empty one.\n        # A registry always exists so runtime ``himport_prepare`` mutates a single object\n        # every connection already shares. The object stays in ``connection_kwargs`` so\n        # it reaches every connection. It is injected unconditionally (like other\n        # auto-added pool kwargs), so a custom ``connection_class`` must accept\n        # ``**kwargs`` (or a ``himport_registry`` parameter), as built-ins do.\n        himport_registry = connection_kwargs.get(\"himport_registry\")\n        if himport_registry is None:\n            himport_registry = HImportRegistry()\n            connection_kwargs[\"himport_registry\"] = himport_registry\n        self.himport_registry = himport_registry\n","sourceCodeStart":2612,"sourceCodeEnd":2648,"githubUrl":"https://github.com/redis/redis-py/blob/6a6b581b48225afa0b76912d1028c6035baee932/redis/asyncio/connection.py#L2612-L2648","documentation":"The async ConnectionPool validates max_connections at construction: max_connections is first coerced via 'or 100' (so 0/None become 100), then if the result is not an int or is negative it raises ValueError. A string, float, bool, or negative number trips it. Note the bound is < 0, so 0 itself does not raise (it becomes 100).","triggerScenarios":"Passing max_connections='10' (string from env/config), max_connections=-1, max_connections=3.5 (float), or max_connections=True/False to ConnectionPool.","commonSituations":"Reading max_connections from an environment variable or YAML/JSON config as a string without casting; float values from computed config; passing a sentinel that is not an int.","solutions":["Pass an int (typically > 0) for max_connections.","Cast config/env values with int(...) before constructing the pool.","Validate the value's type and sign in your own config loader."],"exampleFix":"# before\npool = ConnectionPool(max_connections=os.environ['MAX_CONN'])\n# after\npool = ConnectionPool(max_connections=int(os.environ['MAX_CONN']))","handlingStrategy":"validation","validationCode":"if not isinstance(max_connections, int) or isinstance(max_connections, bool) or max_connections < 0:\n    raise ValueError('max_connections must be a non-negative int')\nmax_connections = max_connections or 100","typeGuard":"def is_valid_max_connections(v) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool) and v >= 0","tryCatchPattern":null,"preventionTips":["Cast env/config-sourced values with int(...) at the config boundary.","Reject floats and strings for pool size in your config loader."],"tags":["config","connection-pool","validation","async"],"backgroundTag":null,"analyzedSha":"6a6b581b48225afa0b76912d1028c6035baee932","analyzedAt":"2026-08-10T12:52:44.840Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}