Skip to content

[ISSUE #10218] Fix resource leak in DumpCompactionLogCommand#10219

Open
daguimu wants to merge 1 commit intoapache:developfrom
daguimu:fix/dumpcompactionlog-resource-leak-10218
Open

[ISSUE #10218] Fix resource leak in DumpCompactionLogCommand#10219
daguimu wants to merge 1 commit intoapache:developfrom
daguimu:fix/dumpcompactionlog-resource-leak-10218

Conversation

@daguimu
Copy link
Copy Markdown

@daguimu daguimu commented Mar 26, 2026

Problem

DumpCompactionLogCommand.execute() creates a RandomAccessFile to obtain a FileChannel, but neither resource is ever closed. The anonymous RandomAccessFile is immediately discarded after .getChannel(), making it impossible to close. This leaks file descriptors on every invocation.

Root Cause

The code uses new RandomAccessFile(fileName, "rw").getChannel() as a one-liner, discarding the RandomAccessFile reference. Neither the RandomAccessFile nor the FileChannel is wrapped in a try-with-resources block or closed in a finally block.

Fix

  • Wrap both RandomAccessFile and FileChannel in a try-with-resources statement to ensure they are always closed after use.
  • Changed the file mode from "rw" to "r" since the command only reads (dumps) the compaction log, it does not need write access. The MapMode was also changed from READ_WRITE to READ_ONLY to match.

Tests Added

Change Point Test
try-with-resources for resource cleanup testExecuteWithValidFile() — writes a message, dumps it, verifies no resource leak (file still accessible)
Read-only mode handles empty files testExecuteWithEmptyFile() — verifies graceful handling of empty files
Non-existent file throws exception testExecuteWithNonExistentFile() — verifies SubCommandException is thrown
Missing -f option testExecuteWithoutFileOption() — verifies error message is printed without exception

Impact

Only affects the dumpCompactionLog CLI tool. No changes to broker runtime or message processing logic.

Fixes #10218

@daguimu daguimu force-pushed the fix/dumpcompactionlog-resource-leak-10218 branch from 05ea91e to 7eaff91 Compare March 27, 2026 01:47
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 76.19048% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 48.87%. Comparing base (932588d) to head (7eaff91).

Files with missing lines Patch % Lines
...ools/command/message/DumpCompactionLogCommand.java 76.19% 3 Missing and 2 partials ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##             develop   #10219      +/-   ##
=============================================
- Coverage      48.96%   48.87%   -0.09%     
+ Complexity     13385    13365      -20     
=============================================
  Files           1373     1373              
  Lines          99904    99907       +3     
  Branches       12901    12902       +1     
=============================================
- Hits           48913    48827      -86     
- Misses         45062    45133      +71     
- Partials        5929     5947      +18     

☔ 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.

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

2 participants