Skip to content

[ISSUE #10218] Fix resource leak in DumpCompactionLogCommand - RandomAccessFile and FileChannel never closed#10228

Open
Senrian wants to merge 1 commit intoapache:developfrom
Senrian:fix/issue-10218-dumpcompactionlogcommand-resource-leak
Open

[ISSUE #10218] Fix resource leak in DumpCompactionLogCommand - RandomAccessFile and FileChannel never closed#10228
Senrian wants to merge 1 commit intoapache:developfrom
Senrian:fix/issue-10218-dumpcompactionlogcommand-resource-leak

Conversation

@Senrian
Copy link
Copy Markdown

@Senrian Senrian commented Mar 29, 2026

Summary

Fixes issue #10218 - DumpCompactionLogCommand.java leaks RandomAccessFile and FileChannel resources.

Bug

On line 74:

FileChannel fileChannel = new RandomAccessFile(fileName, "rw").getChannel();

The anonymous RandomAccessFile instance is immediately discarded after calling .getChannel(), making it impossible to close later. The FileChannel obtained from it is also never closed. Only the ByteBuffer is cleaned up via UtilAll.cleanBuffer(buf).

Fix

Convert to try-with-resources:

try (RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
     FileChannel fileChannel = raf.getChannel()) {
    long fileSize = Files.size(filePath);
    ByteBuffer buf = fileChannel.map(MapMode.READ_WRITE, 0, fileSize);
    // ... rest of the code ...
    UtilAll.cleanBuffer(buf);
} // FileChannel and RandomAccessFile automatically closed here

Why This Matters

  1. File descriptor leak: Each run of DumpCompactionLogCommand leaks one file descriptor
  2. Static analysis: Flagged by SpotBugs, SonarQube and similar tools
  3. Best practice: try-with-resources ensures proper cleanup in all code paths including exceptions

Fixes #10218

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.

[Bug] Resource leak in DumpCompactionLogCommand - RandomAccessFile and FileChannel never closed

1 participant