From bad08176a4c07c865db39349a8cfc781d5595485 Mon Sep 17 00:00:00 2001 From: zhang-arvin Date: Tue, 8 Sep 2026 01:26:55 +0800 Subject: [PATCH] [bug] Fix Self-suppression in FileIO.overwriteFileUtf8 when close rethrows the same exception (#9674) --- .../java/org/apache/paimon/fs/FileIO.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/paimon-common/src/main/java/org/apache/paimon/fs/FileIO.java b/paimon-common/src/main/java/org/apache/paimon/fs/FileIO.java index 2b0dcec3f760..c20e2c70bb18 100644 --- a/paimon-common/src/main/java/org/apache/paimon/fs/FileIO.java +++ b/paimon-common/src/main/java/org/apache/paimon/fs/FileIO.java @@ -378,10 +378,29 @@ default void writeFile(Path path, String content, boolean overwrite) throws IOEx * implementations. */ default void overwriteFileUtf8(Path path, String content) throws IOException { - try (PositionOutputStream out = newOutputStream(path, true)) { + // Some FileIO implementations (e.g. HDFS) rethrow the exact same exception instance from + // close() that was already thrown from write(), which makes the try-with-resources + // suppression mechanism fail with "Self-suppression not permitted". Therefore close the + // stream manually and only add suppressed exceptions that differ from the primary one. + IOException primaryException = null; + PositionOutputStream out = newOutputStream(path, true); + try { OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8); writer.write(content); writer.flush(); + } catch (IOException e) { + primaryException = e; + throw e; + } finally { + try { + out.close(); + } catch (IOException closeException) { + if (primaryException == null) { + throw closeException; + } else if (primaryException != closeException) { + primaryException.addSuppressed(closeException); + } + } } }