{"record":{"id":"397b12a09bc812cb","repo":"apache/iceberg","slug":"unclosed-output-stream-created-by-397b12","errorCode":null,"errorMessage":"Unclosed output stream created by:\n\t{}","messagePattern":"Unclosed output stream created by:\n\t(.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"gcp/src/main/java/org/apache/iceberg/gcp/gcs/GCSOutputStream.java","lineNumber":140,"sourceCode":"  @Override\n  public void close() throws IOException {\n    if (closed) {\n      return;\n    }\n\n    super.close();\n    closed = true;\n    stream.close();\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 = Joiner.on(\"\\n\\t\").join(Arrays.copyOfRange(createStack, 1, createStack.length));\n      LOG.warn(\"Unclosed output stream created by:\\n\\t{}\", trace);\n    }\n  }\n}\n","sourceCodeStart":122,"sourceCodeEnd":144,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/gcp/src/main/java/org/apache/iceberg/gcp/gcs/GCSOutputStream.java#L122-L144","documentation":"GCSOutputStream.finalize detects that an output stream (used when writing files to GCS) was never closed before garbage collection. It closes the stream as a safety net — which for GCS may abort or complete the upload — and logs this warning with the creation stack trace to point at the leak.","triggerScenarios":"Calling GCSFileIO.newOutputFile(...).create(...) or createOrOverwrite(...) and never invoking close() on the returned OutputStream, letting GC finalize it.","commonSituations":"Custom writers that forget try-with-resources; exceptions thrown between create and close; writing metadata/data files in scripts or tests without proper cleanup.","solutions":["Always use try-with-resources around the output stream","Use the stack trace in the warning to locate and fix the unclosed write","Ensure close() (not just flush()) is called so the GCS upload finalizes","Note: without close(), the written file may be incomplete — re-verify data after fixing"],"exampleFix":"// before\nOutputStream out = outF.create();\nout.write(bytes);\n// after\ntry (OutputStream out = outF.create()) {\n  out.write(bytes);\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try (OutputStream out = outputFile.createOrOverwrite()) { out.write(bytes); } catch (IOException e) { throw new UncheckedIOException(e); }","preventionTips":["Always try-with-resources on output streams","Remember close() finalizes the GCS upload","Audit custom writers for early-return paths","Verify written file sizes after fixing leaks"],"tags":["gcs","resource-leak","outputstream","finalize"],"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"}