rocketmq icon indicating copy to clipboard operation
rocketmq copied to clipboard

[Bug] Fix resource leak: properly close RandomAccessFile and FileChannel using try-with-resources

Open Senrian opened this issue 8 months ago • 1 comments

Bug Description

In DumpCompactionLogCommand.java, a RandomAccessFile is created to obtain a FileChannel, but neither the RandomAccessFile nor the FileChannel is ever closed. This causes a file descriptor leak.

Issue: #10218

// Before (buggy):
FileChannel fileChannel = new RandomAccessFile(fileName, "rw").getChannel();
ByteBuffer buf = fileChannel.map(MapMode.READ_WRITE, 0, fileSize);
// ...
UtilAll.cleanBuffer(buf);
// fileChannel and RandomAccessFile are never closed

Fix

Use try-with-resources to ensure both RandomAccessFile and FileChannel are properly closed:

try (RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
     FileChannel fileChannel = raf.getChannel()) {
    // ...
}

This follows the recommended Java practice for resource management.

Senrian avatar Mar 30 '26 04:03 Senrian

Codecov Report

:x: Patch coverage is 0% with 3 lines in your changes missing coverage. Please review. :white_check_mark: Project coverage is 48.93%. Comparing base (35c88e3) to head (e20b3dd). :warning: Report is 23 commits behind head on develop.

Files with missing lines Patch % Lines
...ools/command/message/DumpCompactionLogCommand.java 0.00% 3 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             develop   #10231   +/-   ##
==========================================
  Coverage      48.92%   48.93%           
- Complexity     13375    13379    +4     
==========================================
  Files           1373     1373           
  Lines          99908    99923   +15     
  Branches       12901    12903    +2     
==========================================
+ Hits           48878    48895   +17     
+ Misses         45096    45065   -31     
- Partials        5934     5963   +29     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov-commenter avatar Mar 31 '26 10:03 codecov-commenter