{"record":{"id":"51d3066a05609669","repo":"immich-app/immich","slug":"attempted-to-clear-cache-but-rmtree-is-not-safe-o","errorCode":null,"errorMessage":"Attempted to clear cache, but rmtree is not safe on this platform","messagePattern":"Attempted to clear cache, but rmtree is not safe on this platform","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"machine-learning/immich_ml/models/base.py","lineNumber":92,"sourceCode":"\n        snapshot_download(\n            f\"immich-app/{clean_name(self.model_name)}\",\n            cache_dir=self.cache_dir,\n            local_dir=self.cache_dir,\n            ignore_patterns=ignored_patterns.get(self.model_format, []),\n        )\n\n    def _load(self) -> ModelSession:\n        return self._make_session(self.model_path)\n\n    def clear_cache(self) -> None:\n        if not self.cache_dir.exists():\n            log.warning(\n                f\"Attempted to clear cache for model '{self.model_name}', but cache directory does not exist\",\n            )\n            return\n        if not rmtree.avoids_symlink_attacks:\n            raise RuntimeError(\"Attempted to clear cache, but rmtree is not safe on this platform\")\n\n        if self.cache_dir.is_dir():\n            log.info(f\"Cleared cache directory for model '{self.model_name}'.\")\n            rmtree(self.cache_dir)\n        else:\n            log.warning(\n                (\n                    f\"Encountered file instead of directory at cache path \"\n                    f\"for '{self.model_name}'. Removing file and replacing with a directory.\"\n                ),\n            )\n            self.cache_dir.unlink()\n        self.cache_dir.mkdir(parents=True, exist_ok=True)\n\n    def _make_session(self, model_path: Path) -> ModelSession:\n        if not model_path.is_file():\n            raise FileNotFoundError(f\"Model file not found: {model_path}\")\n","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/immich-app/immich/blob/199723261c6ffa897fec8ccdaea6359e39c37cc3/machine-learning/immich_ml/models/base.py#L74-L110","documentation":"Raised as RuntimeError by InferenceModel.clear_cache (models/base.py) when the cache directory exists but shutil.rmtree's rmtree.avoids_symlink_attacks is False on the current platform — i.e. the OS cannot guarantee rmtree is safe against symlink attacks, so Immich refuses to delete to avoid a potential security issue.","triggerScenarios":"clear_cache is invoked (e.g. during model reload/cache invalidation) on a platform where shutil does not advertise symlink-attack-safe rmtree (historically some non-Windows setups, or older Python versions where the flag is not set).","commonSituations":"Running the ML service on an unusual filesystem or an older Python build where rmtree.avoids_symlink_attacks evaluates False; a hardened/sandboxed environment that strips the symlink guarantee; manually triggering cache clear on an unsupported platform.","solutions":["Run the ML container on a mainstream Linux filesystem/Python where rmtree.avoids_symlink_attacks is True (the default on modern CPython).","Manually delete the cache directory from the host (docker exec / volume) instead of relying on clear_cache, then restart the service.","Upgrade the Python/OS so the symlink-safety flag is set."],"exampleFix":"# before — calling clear_cache on an unsafe platform\nmodel.clear_cache()  # raises RuntimeError\n\n# after — remove cache out-of-band, then reload\n# docker exec immich-machine-learning rm -rf /cache/<model>\n# then restart the container / let the model re-download","handlingStrategy":"fallback","validationCode":"import shutil\nfrom pathlib import Path\ndef safe_clear_cache(cache_dir: Path) -> None:\n    if not cache_dir.exists(): return\n    if not shutil.rmtree.avoids_symlink_attacks:\n        raise RuntimeError('rmtree unsafe on this platform')\n    shutil.rmtree(cache_dir)","typeGuard":"def rmtree_is_safe() -> bool:\n    import shutil\n    return bool(getattr(shutil.rmtree, 'avoids_symlink_attacks', False))","tryCatchPattern":"try:\n    model.clear_cache()\nexcept RuntimeError as e:\n    if 'not safe on this platform' in str(e):\n        # remove out-of-band, then reload\n        import subprocess; subprocess.run(['rm', '-rf', str(model.cache_dir)])\n    else: raise","preventionTips":["Run the ML container on a mainstream Linux + modern Python where rmtree.avoids_symlink_attacks is True.","Manage the cache volume from the host rather than relying on clear_cache on edge platforms."],"tags":["machine-learning","cache","filesystem","security","python","runtimeerror"],"backgroundTag":null,"analyzedSha":"199723261c6ffa897fec8ccdaea6359e39c37cc3","analyzedAt":"2026-08-12T04:54:27.085Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}