{"record":{"id":"072d5d57c00d2d1c","repo":"MuntashirAkon/AppManager","slug":"could-not-create-cache-is-this-os-broken","errorCode":null,"errorMessage":"Could not create cache. Is this OS broken?","messagePattern":"Could not create cache\\. Is this OS broken\\?","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/io/github/muntashirakon/AppManager/self/filecache/FileCache.java","lineNumber":54,"sourceCode":"\n    private final File mCacheDir;\n    private final Map<Path, File> mFileCacheMap = new HashMap<>();\n    private final Set<File> mFileCache = new HashSet<>();\n    private final Set<File> mDirectoryCache = new HashSet<>();\n    private final boolean mSessionOnly;\n\n    private boolean mClosed = false;\n\n    public FileCache() {\n        this(true);\n    }\n\n    private FileCache(boolean sessionOnly) {\n        mSessionOnly = sessionOnly;\n        mCacheDir = new File(FileUtils.getCachePath(), \"files\");\n        if (!mCacheDir.exists()) {\n            if (!mCacheDir.mkdirs()) {\n                throw new IllegalStateException(\"Could not create cache. Is this OS broken?\");\n            }\n        }\n    }\n\n    @Override\n    public void close() {\n        mClosed = true;\n        if (mSessionOnly) {\n            deleteAll();\n        }\n    }\n\n    @Override\n    protected void finalize() {\n        if (!mClosed) {\n            close();\n        }\n    }","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/app/src/main/java/io/github/muntashirakon/AppManager/self/filecache/FileCache.java#L36-L72","documentation":"The private FileCache constructor creates <cache-path>/files on first use and throws IllegalStateException(\"Could not create cache. Is this OS broken?\") if mkdirs() fails. It assumes cache-directory creation can only fail in a fundamentally broken environment, hiding the real cause (storage full, permission denied, path unavailable).","triggerScenarios":"Instantiating FileCache when the cache path returned by FileUtils.getCachePath() cannot be created: storage full, unwritable parent, cache dir unavailable in restricted users/profiles, or emulated-storage failures.","commonSituations":"Device storage completely full, app cache deleted with unwritable parent, multi-user/work/restricted profiles denying cache access, FUSE/emulated-storage failures on some ROMs, cloned apps in isolated profiles.","solutions":["Check free storage space; free space or clear the app cache so the directory can be recreated.","Log FileUtils.getCachePath() when mkdirs fails to identify the real cause (the boolean return hides errno).","Catch IllegalStateException at cache-consumer call sites and disable caching features gracefully.","Restart the app/runtime if the cache path is only temporarily unavailable.","On restricted profiles, ensure the app's cache directories are accessible or fall back to internal cache."],"exampleFix":"// before\nif (!mCacheDir.exists()) {\n    if (!mCacheDir.mkdirs()) {\n        throw new IllegalStateException(\"Could not create cache. Is this OS broken?\");\n    }\n}\n// after\nif (!mCacheDir.exists() && !mCacheDir.mkdirs()) {\n    File fallback = new File(ContextUtils.getContext().getCacheDir(), \"files\");\n    if (fallback.equals(mCacheDir) || !fallback.mkdirs()) {\n        throw new IllegalStateException(\"Could not create cache at \" + mCacheDir);\n    }\n    mCacheDir = fallback;\n}","handlingStrategy":"try-catch","validationCode":"File cacheDir = new File(FileUtils.getCachePath(), \"files\");\nboolean ok = cacheDir.isDirectory()\n        || (cacheDir.getParentFile() != null\n            && cacheDir.getParentFile().canWrite()\n            && cacheDir.mkdirs());\nif (!ok) {\n    // storage full or unwritable: disable cache-dependent features first\n}","typeGuard":null,"tryCatchPattern":"try {\n    FileCache cache = FileCache.getInstance();\n    // use cache\n} catch (IllegalStateException e) {\n    // cache unavailable: skip caching, free storage space, or clear app cache\n}","preventionTips":["Monitor free disk space before performing cache-heavy operations.","Clear stale cache entries regularly so cache-dir recreation is rarely needed.","Test on restricted/work profiles and low-storage devices.","Log FileUtils.getCachePath() on failure to distinguish full storage vs permission issues."],"tags":["android","cache","filesystem","storage"],"backgroundTag":"mkdir-permission-denied","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}