This is an automated email from the ASF dual-hosted git repository.

chia7712 pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/kafka.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 01ba1b2d4db MINOR: Fix RandomAccessFile leak in 
FileRecords.openChannel (#22878)
01ba1b2d4db is described below

commit 01ba1b2d4db7ce14dd4df7bc71dbb97db75e2666
Author: David Simon <[email protected]>
AuthorDate: Tue Jul 21 07:48:19 2026 +0200

    MINOR: Fix RandomAccessFile leak in FileRecords.openChannel (#22878)
    
    `randomAccessFile` is leaked when `setLength` throws. This change wraps
    the call in a try-catch that closes the file before re-throwing.
    
    Reviewers: Chia-Ping Tsai <[email protected]>
---
 .../org/apache/kafka/common/record/internal/FileRecords.java     | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git 
a/clients/src/main/java/org/apache/kafka/common/record/internal/FileRecords.java
 
b/clients/src/main/java/org/apache/kafka/common/record/internal/FileRecords.java
index f62ecd3c2b4..7a7ae0f4e00 100644
--- 
a/clients/src/main/java/org/apache/kafka/common/record/internal/FileRecords.java
+++ 
b/clients/src/main/java/org/apache/kafka/common/record/internal/FileRecords.java
@@ -487,8 +487,13 @@ public class FileRecords extends AbstractRecords 
implements Closeable {
                         StandardOpenOption.WRITE);
             } else {
                 RandomAccessFile randomAccessFile = new RandomAccessFile(file, 
"rw");
-                randomAccessFile.setLength(initFileSize);
-                return randomAccessFile.getChannel();
+                try {
+                    randomAccessFile.setLength(initFileSize);
+                    return randomAccessFile.getChannel();
+                } catch (IOException e) {
+                    randomAccessFile.close();
+                    throw e;
+                }
             }
         } else {
             return FileChannel.open(file.toPath());

Reply via email to