Skip to content

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

Open
Senrian wants to merge 2 commits intoapache:developfrom
Senrian:fix-resource-leak-dump-compaction-log
Open

[Bug] Fix resource leak: properly close RandomAccessFile and FileChannel using try-with-resources#10231
Senrian wants to merge 2 commits intoapache:developfrom
Senrian:fix-resource-leak-dump-compaction-log

Conversation

@Senrian
Copy link
Copy Markdown

@Senrian Senrian commented Mar 30, 2026

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.

Copy link
Copy Markdown
Member

@lizhimins lizhimins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use read only mode

}

try {
try (RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use read only mode

Fix reviewer feedback: use read-only mode ("r") since the file is only being read, not written.
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 48.93%. Comparing base (35c88e3) to head (e20b3dd).
⚠️ Report is 7 commits behind head on develop.

Files with missing lines Patch % Lines
...ools/command/message/DumpCompactionLogCommand.java 0.00% 3 Missing ⚠️
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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants