From 05f2e39cf3add09501c1534328db2452370a582c Mon Sep 17 00:00:00 2001 From: hellishfire Date: Mon, 22 Jul 2024 15:49:12 +0800 Subject: [PATCH] GH-2935: Avoid double close of ParquetFileWriter (#2951) * GH-2935: Avoid double close of ParquetFileWriter * fix comment --------- Co-authored-by: youming.whl --- .../java/org/apache/parquet/hadoop/ParquetFileWriter.java | 6 ++++++ .../org/apache/parquet/hadoop/TestParquetFileWriter.java | 3 +++ 2 files changed, 9 insertions(+) diff --git a/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ParquetFileWriter.java b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ParquetFileWriter.java index db15ba406a..814b98c50f 100644 --- a/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ParquetFileWriter.java +++ b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ParquetFileWriter.java @@ -170,6 +170,7 @@ public static enum Mode { // set when end is called private ParquetMetadata footer = null; + private boolean closed; private final CRC32 crc; private final ReusingByteBufferAllocator crcAllocator; @@ -1658,11 +1659,16 @@ public void end(Map extraMetaData) throws IOException { @Override public void close() throws IOException { + if (closed) { + return; + } try (PositionOutputStream temp = out) { temp.flush(); if (crcAllocator != null) { crcAllocator.close(); } + } finally { + closed = true; } } diff --git a/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestParquetFileWriter.java b/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestParquetFileWriter.java index 32e2eac0b9..c6be72ff70 100644 --- a/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestParquetFileWriter.java +++ b/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestParquetFileWriter.java @@ -286,6 +286,9 @@ public void testWriteRead() throws Exception { w.endColumn(); w.endBlock(); w.end(new HashMap()); + // Although writer is already closed in previous end(), + // explicitly close it again to verify double close behavior. + w.close(); ParquetMetadata readFooter = ParquetFileReader.readFooter(configuration, path); assertEquals("footer: " + readFooter, 2, readFooter.getBlocks().size());