{"record":{"id":"deb5919a7bfff65f","repo":"iBotPeaches/Apktool","slug":"could-not-delete-tmp-file","errorCode":null,"errorMessage":"Could not delete tmp file: ","messagePattern":"Could not delete tmp file: ","errorType":"exception","errorClass":"BrutException","httpStatus":null,"severity":"warning","filePath":"brut.j.util/src/main/java/brut/util/OS.java","lineNumber":183,"sourceCode":"            process.waitFor(15, TimeUnit.SECONDS);\n            executor.shutdownNow();\n\n            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;","sourceCodeStart":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/iBotPeaches/Apktool/blob/79b63384d7d7e22917e6ea8b453272da7012515b/brut.j.util/src/main/java/brut/util/OS.java#L165-L201","documentation":"Thrown by OS.createTempDirectory when File.createTempFile('BRUT', null) succeeds but the immediate tmp.delete() on the just-created file returns false. The method's contract is: create a placeholder file, delete it, then mkdir the directory in its place. A false from delete() means something else holds or recreated the file — typically antivirus software on Windows, or a race with another process scanning java.io.tmpdir.","triggerScenarios":"OS.createTempDirectory() on systems where the freshly created temp file cannot be deleted: real-time antivirus holding an exclusive handle (Windows Defender, corporate AV), NFS-mounted tmpdir with delayed attribute caching, or a file watcher recreating/locking files in the temp directory.","commonSituations":"Windows workstations with aggressive antivirus scanning temp files the instant they appear; corporate environments with endpoint protection; tmpdir pointed at a network share (-Djava.io.tmpdir); CI agents where other jobs race through a shared temp dir.","solutions":["Add an AV exclusion for the java.io.tmpdir / build directory, or switch AV to on-access scan on close","Point java.io.tmpdir to a local, non-scanned directory: -Djava.io.tmpdir=/var/tmp/mybuild","Retry once after a short delay — the lock is usually transient","Replace the pattern in your own code: Files.createTempDirectory(\"BRUT\") creates the directory atomically without the delete-then-mkdir dance"],"exampleFix":"// before (library internals)\nFile tmp = File.createTempFile(\"BRUT\", null);\ntmp.delete();      // false under AV lock -> BrutException\n\n// after (your own equivalent, avoids the failure mode)\nPath tmp = Files.createTempDirectory(\"BRUT\");\nFile dir = tmp.toFile();","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"File dir = null;\nfor (int i = 0; i < 2 && dir == null; i++) {\n    try {\n        dir = OS.createTempDirectory();\n    } catch (BrutException e) {\n        if (i == 1 || !e.getMessage().startsWith(\"Could not delete tmp file\")) throw e;\n    }\n}","preventionTips":["Exclude the build/temp directories from real-time antivirus scanning on Windows","Set -Djava.io.tmpdir to a local fast disk, never a network share","Prefer Files.createTempDirectory in your own code — it skips the delete-then-mkdir race entirely"],"tags":["filesystem","temp-files","antivirus","windows","java"],"backgroundTag":null,"analyzedSha":"79b63384d7d7e22917e6ea8b453272da7012515b","analyzedAt":"2026-08-14T10:43:28.812Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}