{"record":{"id":"6ba1aecab22823f0","repo":"HumanSignal/label-studio","slug":"please-explicitly-pass-a-redis-db-id-to-prevent-ac","errorCode":null,"errorMessage":"Please explicitly pass a redis db id to prevent accidentally overwriting existing database!","messagePattern":"Please explicitly pass a redis db id to prevent accidentally overwriting existing database!","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"label_studio/io_storages/redis/models.py","lineNumber":57,"sourceCode":"    )\n\n    def get_redis_connection(self, db=None, redis_config={}):\n        \"\"\"Get a redis connection from the provided arguments.\n\n        Args:\n            db (int): Database ID of database to use. This needs to\n                      always be provided to prevent accidental overwrite\n                      to a default value. Therefore, the default is None,\n                      but raises an error if not provided.\n            redis_config (dict, optional): Further redis configuration.\n\n        Returns:\n            redis.StrictRedis object with connection to database.\n        \"\"\"\n        if not db:\n            # This should never happen, but better to check than to accidentally\n            # overwrite an existing database by choosing a wrong default:\n            raise ValueError(\n                'Please explicitly pass a redis db id to prevent accidentally overwriting existing database!'\n            )\n\n        # Since tasks are always text, we use StrictRedis with utf-8 decoding.\n        r = redis.StrictRedis(db=db, charset='utf-8', decode_responses=True, **redis_config)\n        # Test connection\n        # (this will raise redis.exceptions.ConnectionError if it cannot connect)\n        r.ping()\n        return r\n\n    def get_client(self):\n        redis_config = {}\n        if self.host:\n            redis_config['host'] = self.host\n        if self.port:\n            redis_config['port'] = self.port\n        if self.password:\n            redis_config['password'] = self.password","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/HumanSignal/label-studio/blob/0b49e9b53917880baf1dd85d574fe5541a9aafb2/label_studio/io_storages/redis/models.py#L39-L75","documentation":"get_redis_connection() in label_studio/io_storages/redis/models.py deliberately refuses to connect when no Redis database number (`db`) is supplied. Redis defaults to db 0, and Label Studio could silently overwrite tasks stored there by another app, so the guard forces you to choose the db explicitly. It is a fail-fast ValueError raised before any network I/O.","triggerScenarios":"Calling get_redis_connection(db=None) (or falsy db such as 0) with only host/port/password in redis_config — e.g. constructing RedisImportStorage without the `db` field, or passing db via a template that renders empty.","commonSituations":"Setting only REDIS_HOST/REDIS_PORT env vars and forgetting REDIS_DB; programmatically instantiating RedisImportStorage(**data) where the serializer was never used so `db` was never required; copying a redis:// URL into config but not extracting the db number.","solutions":["Pass an explicit db argument, e.g. get_redis_connection(db=1, redis_config={...}).","When using the API/serializer, include the `db` field in the storage creation payload.","If db comes from an env var, ensure REDIS_DB (or equivalent) is set and non-empty before constructing the storage."],"exampleFix":"// before\nr = get_redis_connection(redis_config={'host': 'localhost', 'port': 6379})\n\n// after\nr = get_redis_connection(db=1, redis_config={'host': 'localhost', 'port': 6379})","handlingStrategy":"validation","validationCode":"def ensure_redis_db(config: dict) -> int:\n    db = config.get('db') or int(os.environ.get('REDIS_DB', 0))\n    if not db:\n        raise ValueError('Set an explicit Redis db (e.g. REDIS_DB=1) before connecting.')\n    return db\n\n# call before storage creation\ndb = ensure_redis_db(storage_config)","typeGuard":"def has_redis_db(cfg: dict) -> bool:\n    db = cfg.get('db')\n    return isinstance(db, int) and db > 0","tryCatchPattern":null,"preventionTips":["Always set REDIS_DB (non-zero) in deployment env vars.","Never call get_redis_connection without an explicit db kwarg.","Use the RedisImportStorageSerializer so `db` is enforced at the API boundary.","Keep a dedicated db number per application sharing the Redis instance."],"tags":["redis","configuration","missing-argument","validation"],"backgroundTag":"missing-required-config","analyzedSha":"0b49e9b53917880baf1dd85d574fe5541a9aafb2","analyzedAt":"2026-08-29T00:39:52.578Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}