{"record":{"id":"f2340f30c1a40868","repo":"apache/druid","slug":"could-not-create-temporary-file-s-for-copying","errorCode":null,"errorMessage":"Could not create temporary file [%s] for copying [%s]","messagePattern":"Could not create temporary file \\[(.+?)\\] for copying \\[(.+?)\\]","errorType":"exception","errorClass":"org.apache.druid.java.util.common.IOE","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/storage/remote/ChunkingStorageConnector.java","lineNumber":143,"sourceCode":"          {\n            if (!initStream) {\n              initStream = true;\n              return new NullInputStream();\n            }\n\n            File outFile = new File(\n                params.getTempDirSupplier().get().getAbsolutePath(),\n                UUID.randomUUID().toString()\n            );\n\n            long currentReadEndPosition = Math.min(\n                currentReadStartPosition.get() + chunkSizeBytes,\n                readEnd\n            );\n\n            try {\n              if (!outFile.createNewFile()) {\n                throw new IOE(\n                    StringUtils.format(\n                        \"Could not create temporary file [%s] for copying [%s]\",\n                        outFile.getAbsolutePath(),\n                        params.getCloudStoragePath()\n                    )\n                );\n              }\n\n              FileUtils.copyLarge(\n                  () -> new RetryingInputStream<>(\n                      params.getObjectSupplier().getObject(currentReadStartPosition.get(), currentReadEndPosition),\n                      params.getObjectOpenFunction(),\n                      params.getRetryCondition(),\n                      params.getMaxRetry()\n                  ),\n                  outFile,\n                  new byte[FETCH_BUFFER_SIZE_BYTES],\n                  Predicates.alwaysFalse(),","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/storage/remote/ChunkingStorageConnector.java#L125-L161","documentation":"ChunkingStorageConnector's chunk-copy iterator creates a temporary file with File.createNewFile() before streaming each chunk. If the JDK reports the file already exists (createNewFile returns false) it throws this IOE. createNewFile also returns false if the file was concurrently created or the check-and-create atomically fails.","triggerScenarios":"Copying a cloud-storage object where the computed temp file path (in the given temp dir) already exists — e.g. a previous failed copy left the temp file behind, or two concurrent copies of the same chunk compute the same temp file name in a shared temp directory.","commonSituations":"Multiple Druid processes or retries sharing one tmp dir (same java.io.tmpdir or configured tmpDir); leftover temp files after a crash; concurrent segment downloads of the same segment to a shared cache directory.","solutions":["Clear the temp directory of stale files from previous failed copies","Ensure each copy uses a unique temp directory or unique file names (UUID per attempt)","Avoid running concurrent copies of the same object into the same temp dir"],"exampleFix":"// before\nFile outFile = new File(sharedTmpDir, \"chunk-\" + chunkIndex);\n// after\nFile outFile = File.createTempFile(\"chunk-\" + chunkIndex + \"-\", \".tmp\", tmpDir);","handlingStrategy":"validation","validationCode":"File outFile = new File(tmpDir, name);\nif (outFile.exists()) {\n  outFile = File.createTempFile(\"chunk-\", \".tmp\", tmpDir);\n}","typeGuard":null,"tryCatchPattern":"try { copyChunk(...); } catch (IOE e) { // temp file exists: use unique name or clean tmp dir }","preventionTips":["Use File.createTempFile or UUID-suffixed names to avoid collisions","Clean stale temp files after crashes before reusing a tmp dir","Do not share one temp dir across concurrent copies of the same object"],"tags":["filesystem","temp-file","concurrency"],"backgroundTag":"file-already-exists","analyzedSha":"9b90983fd291f26935af934383ce360473179e4d","analyzedAt":"2026-09-07T13:32:30.957Z","contentChangedAt":"2026-09-07T13:32:30.957Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}