{"record":{"id":"7d4ba090a942a2e2","repo":"iBotPeaches/Apktool","slug":"could-not-create-tmp-dir","errorCode":null,"errorMessage":"Could not create tmp dir: ","messagePattern":"Could not create tmp dir: ","errorType":"exception","errorClass":"BrutException","httpStatus":null,"severity":"warning","filePath":"brut.j.util/src/main/java/brut/util/OS.java","lineNumber":186,"sourceCode":"            if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {\n                Log.w(TAG, \"Stream collector did not terminate.\");\n            }\n            return collector.get();\n        } catch (IOException | InterruptedException ignored) {\n            return null;\n        }\n    }\n\n    public static File createTempDirectory() throws BrutException {\n        try {\n            File tmp = File.createTempFile(\"BRUT\", null);\n            tmp.deleteOnExit();\n\n            if (!tmp.delete()) {\n                throw new BrutException(\"Could not delete tmp file: \" + tmp.getAbsolutePath());\n            }\n            if (!tmp.mkdir()) {\n                throw new BrutException(\"Could not create tmp dir: \" + tmp.getAbsolutePath());\n            }\n\n            return tmp;\n        } catch (IOException ex) {\n            throw new BrutException(\"Could not create tmp dir\", ex);\n        }\n    }\n\n    private static class StreamForwarder extends Thread {\n        private final InputStream mIn;\n        private final String mType;\n\n        public StreamForwarder(InputStream in, String type) {\n            mIn = in;\n            mType = type;\n        }\n\n        @Override","sourceCodeStart":168,"sourceCodeEnd":204,"githubUrl":"https://github.com/iBotPeaches/Apktool/blob/79b63384d7d7e22917e6ea8b453272da7012515b/brut.j.util/src/main/java/brut/util/OS.java#L168-L204","documentation":"Thrown by OS.createTempDirectory when the placeholder temp file was deleted but tmp.mkdir() immediately returns false. mkdir() fails without throwing when a directory already exists at that path, a file was recreated there in the microsecond between delete and mkdir (file-watcher/AV race), or the parent is not writable. The absolute path of the failed directory is included in the message.","triggerScenarios":"OS.createTempDirectory() where the createTempFile/delete/mkdir sequence loses a race against antivirus recreating the deleted file, another process creating the same directory (predictable 'BRUT' prefix collisions), or the temp directory permissions being revoked mid-run.","commonSituations":"Antivirus that quarantines and re-materializes the deleted temp file; parallel apktool instances or threads hammering the same temp dir; tmpdir on a full or quota-limited filesystem; containers with a read-only tmpfs after a security policy change.","solutions":["Retry with a fresh call — createTempFile generates a new random name each attempt, so transient races usually clear","Disable/exclude temp-dir scanning in AV, or relocate tmpdir with -Djava.io.tmpdir to a local path","Check the parent directory state: Files.isWritable(Paths.get(System.getProperty(\"java.io.tmpdir\")))","Use Files.createTempDirectory(\"BRUT\") in your own code — it is atomic and immune to this delete/mkdir race"],"exampleFix":"// before\nFile dir = OS.createTempDirectory(); // mkdir loses race with AV\n\n// after\nFile dir;\ntry {\n    dir = OS.createTempDirectory();\n} catch (BrutException e) { // transient race: one retry with a new random name\n    dir = OS.createTempDirectory();\n}","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    return OS.createTempDirectory();\n} catch (BrutException e) {\n    if (e.getMessage().startsWith(\"Could not create tmp dir\")) {\n        return OS.createTempDirectory(); // new random name, race usually gone\n    }\n    throw e;\n}","preventionTips":["Retry once: each attempt uses a fresh random 'BRUT' name, so name collisions and AV races rarely repeat","Avoid pointing tmpdir at watched/scanned folders when many short-lived temp dirs are created","Verify temp-dir writability at startup: Files.isWritable(Paths.get(System.getProperty(\"java.io.tmpdir\")))"],"tags":["filesystem","temp-files","race-condition","antivirus","java"],"backgroundTag":null,"analyzedSha":"79b63384d7d7e22917e6ea8b453272da7012515b","analyzedAt":"2026-08-14T10:43:28.812Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}