{"record":{"id":"9ebd274bebba7cc9","repo":"apache/druid","slug":"cannot-delete-temp-file-s","errorCode":null,"errorMessage":"Cannot delete temp file [%s]","messagePattern":"Cannot delete temp file \\[(.+?)\\]","errorType":"exception","errorClass":"org.apache.druid.java.util.common.RE","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/storage/remote/ChunkingStorageConnector.java","lineNumber":189,"sourceCode":"              throw new RE(e, StringUtils.format(\"Unable to copy [%s] to [%s]\", params.getCloudStoragePath(), outFile));\n            }\n\n            try {\n              AtomicBoolean fileInputStreamClosed = new AtomicBoolean(false);\n              return new FileInputStream(outFile)\n              {\n                @Override\n                public void close() throws IOException\n                {\n                  // close should be idempotent\n                  if (fileInputStreamClosed.get()) {\n                    return;\n                  }\n                  fileInputStreamClosed.set(true);\n                  super.close();\n                  currentReadStartPosition.set(currentReadEndPosition);\n                  if (!outFile.delete()) {\n                    throw new RE(\"Cannot delete temp file [%s]\", outFile);\n                  }\n                }\n\n              };\n            }\n            catch (FileNotFoundException e) {\n              throw new RE(e, StringUtils.format(\"Unable to find temp file [%s]\", outFile));\n            }\n          }\n        }\n    )\n    {\n      @Override\n      public void close() throws IOException\n      {\n        isSequenceStreamClosed.set(true);\n        super.close();\n      }","sourceCodeStart":171,"sourceCodeEnd":207,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/storage/remote/ChunkingStorageConnector.java#L171-L207","documentation":"When the returned FileInputStream over the temp chunk file is closed, the connector deletes outFile and throws this RuntimeException if File.delete() returns false. delete() fails when the file cannot be removed, e.g. another open handle on the same file or OS-level restrictions.","triggerScenarios":"Closing the stream returned by ChunkingStorageConnector.getSegment/getNextElement when the temp file cannot be deleted — typically because another thread/process still holds the file open, or on some platforms the delete races with a concurrent reader.","commonSituations":"Multiple consumers sharing one chunk file (a second reader still holds an fd); file locked by indexing/scanning software; NFS exports where unlink semantics misbehave; leftover handles after a leaked stream that was never closed.","solutions":["Ensure every consumer closes its own stream promptly and no handles are leaked","Use a unique temp file per copy so concurrent readers do not share one file","Manually delete the stale temp file after the process stops holding it"],"exampleFix":"// ensure prompt close\ntry (FileInputStream in = connector.getSegment(path)) {\n  consume(in);\n} // close() deletes the temp file here, no leaked handles","handlingStrategy":"try-catch","validationCode":"// ensure no other handles before close\nboolean stillOpen = otherReaders.stream().anyMatch(r -> !r.isClosed());","typeGuard":null,"tryCatchPattern":"try (FileInputStream in = connector.getSegment(...)) { consume(in); } catch (RuntimeException e) { // delete failed: check for leaked handles, clean up manually }","preventionTips":["Give each consumer its own temp file and stream","Always close streams in try-with-resources to avoid leaked fds","Exclude Druid temp dirs from file-locking/AV scanners"],"tags":["filesystem","temp-file","cleanup"],"backgroundTag":"file-delete-failed","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"}