{"record":{"id":"f8d283f00eaefc73","repo":"apache/iceberg","slug":"unclosed-input-stream-created-by-f8d283","errorCode":null,"errorMessage":"Unclosed input stream created by:\n\t{}","messagePattern":"Unclosed input stream created by:\n\t(.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"core/src/main/java/org/apache/iceberg/hadoop/HadoopStreams.java","lineNumber":133,"sourceCode":"    @Override\n    public int read() throws IOException {\n      return stream.read();\n    }\n\n    @Override\n    public int read(byte[] b, int off, int len) throws IOException {\n      return stream.read(b, off, len);\n    }\n\n    @SuppressWarnings({\"checkstyle:NoFinalizer\", \"Finalize\", \"deprecation\"})\n    @Override\n    protected void finalize() throws Throwable {\n      super.finalize();\n      if (!closed) {\n        close(); // releasing resources is more important than printing the warning\n        String trace =\n            Joiner.on(\"\\n\\t\").join(Arrays.copyOfRange(createStack, 1, createStack.length));\n        LOG.warn(\"Unclosed input stream created by:\\n\\t{}\", trace);\n      }\n    }\n  }\n\n  /** PositionOutputStream implementation for FSDataOutputStream. */\n  private static class HadoopPositionOutputStream extends PositionOutputStream\n      implements DelegatingOutputStream {\n    private final FSDataOutputStream stream;\n    private final StackTraceElement[] createStack;\n    private boolean closed;\n\n    HadoopPositionOutputStream(FSDataOutputStream stream) {\n      this.stream = stream;\n      this.createStack = Thread.currentThread().getStackTrace();\n      this.closed = false;\n    }\n\n    @Override","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/core/src/main/java/org/apache/iceberg/hadoop/HadoopStreams.java#L115-L151","documentation":"HadoopStreams wraps Hadoop FSDataInputStream as Iceberg SeekableInputStream. When such a stream is garbage-collected without close() being called, finalize() closes it and logs a WARN containing the stack trace captured at creation, indicating the caller leaked a file handle. Unclosed streams can exhaust filesystem/socket resources.","triggerScenarios":"A reader (e.g. Avro/Parquet reader or scan task) opens table.io().newInputFile(...).newStream() and is not closed — e.g. a CloseableIterable not fully consumed/closed, an exception path skipping close, or user code holding the stream without try-with-resources.","commonSituations":"Custom scan/reader code that breaks out of iteration early without closing; exceptions during read that skip finally blocks; streaming results to a consumer that never closes the reader.","solutions":["Wrap stream-producing constructs in try-with-resources and close CloseableIterables/Readers in finally.","Ensure callers close SeekableInputStream explicitly; do not rely on GC/finalize.","Check for early returns/exceptions between open and close in custom reader code; add close in finally.","Heap-dump or use the logged creation stack trace to find the leaking call site."],"exampleFix":"// before\nSeekableInputStream in = file.newStream();\nAvroIterable<T> it = Avro.read(in).build();\nreturn it.iterator();\n// after\ntry (AvroIterable<T> it = Avro.read(file.newStream()).build()) {\n  return ImmutableList.copyOf(it);\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try (SeekableInputStream in = file.newStream()) {\n  // read\n} // auto-closed even on exception","preventionTips":["Always use try-with-resources for streams and iterables.","Close CloseableIterable/CloseableIterator in finally blocks.","Never rely on GC to release file handles.","Investigate any 'Unclosed input stream' WARN immediately using its stack trace."],"tags":["resource-leak","stream","hadoop"],"backgroundTag":"resource-leak","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}