From 59bddc22648ece115a24592e45c0fd444d7e26a4 Mon Sep 17 00:00:00 2001 From: Senrian <47714364+Senrian@users.noreply.github.com> Date: Mon, 30 Mar 2026 12:28:07 +0800 Subject: [PATCH 1/2] Fix resource leak: properly close RandomAccessFile and FileChannel using try-with-resources --- .../tools/command/message/DumpCompactionLogCommand.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/src/main/java/org/apache/rocketmq/tools/command/message/DumpCompactionLogCommand.java b/tools/src/main/java/org/apache/rocketmq/tools/command/message/DumpCompactionLogCommand.java index eee8f3d4bde..3941f5021ce 100644 --- a/tools/src/main/java/org/apache/rocketmq/tools/command/message/DumpCompactionLogCommand.java +++ b/tools/src/main/java/org/apache/rocketmq/tools/command/message/DumpCompactionLogCommand.java @@ -69,9 +69,9 @@ public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throw new SubCommandException("file " + fileName + " is a directory."); } - try { + try (RandomAccessFile raf = new RandomAccessFile(fileName, "rw"); + FileChannel fileChannel = raf.getChannel()) { long fileSize = Files.size(filePath); - FileChannel fileChannel = new RandomAccessFile(fileName, "rw").getChannel(); ByteBuffer buf = fileChannel.map(MapMode.READ_WRITE, 0, fileSize); int current = 0; From e20b3dd82e69a27b62d1fc89a0c941123b64f4d3 Mon Sep 17 00:00:00 2001 From: Senrian <47714364+Senrian@users.noreply.github.com> Date: Mon, 30 Mar 2026 17:51:14 +0800 Subject: [PATCH 2/2] Use read-only mode for RandomAccessFile and FileChannel Fix reviewer feedback: use read-only mode ("r") since the file is only being read, not written. --- .../tools/command/message/DumpCompactionLogCommand.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/src/main/java/org/apache/rocketmq/tools/command/message/DumpCompactionLogCommand.java b/tools/src/main/java/org/apache/rocketmq/tools/command/message/DumpCompactionLogCommand.java index 3941f5021ce..355379221f0 100644 --- a/tools/src/main/java/org/apache/rocketmq/tools/command/message/DumpCompactionLogCommand.java +++ b/tools/src/main/java/org/apache/rocketmq/tools/command/message/DumpCompactionLogCommand.java @@ -69,10 +69,10 @@ public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throw new SubCommandException("file " + fileName + " is a directory."); } - try (RandomAccessFile raf = new RandomAccessFile(fileName, "rw"); + try (RandomAccessFile raf = new RandomAccessFile(fileName, "r"); FileChannel fileChannel = raf.getChannel()) { long fileSize = Files.size(filePath); - ByteBuffer buf = fileChannel.map(MapMode.READ_WRITE, 0, fileSize); + ByteBuffer buf = fileChannel.map(MapMode.READ_ONLY, 0, fileSize); int current = 0; while (current < fileSize) {