apache / apache/rocketmq

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

Open Beginner friendly
#10,218 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
22.6k
Forks
12k
Avg merge
2d 20h
Merged PRs (30d)
26

Description

### Before Creating the Bug Report

- [x] I found a bug, not just asking a question, which should be created in [GitHub Discussions](https://github.com/apache/rocketmq/discussions).

- [x] I have searched the [GitHub Issues](https://github.com/apache/rocketmq/issues) and [GitHub Discussions](https://github.com/apache/rocketmq/discussions) of this repository and believe that this is not a duplicate.

- [x] I have confirmed that this bug belongs to the current repository, not other repositories of RocketMQ.

### Runtime platform environment

All platforms

### RocketMQ version

develop branch (latest)

### JDK Version

JDK 8+

### Describe the Bug

In `DumpCompactionLogCommand.java`, a `RandomAccessFile` is created to obtain a `FileChannel`, but neither the `RandomAccessFile` nor the `FileChannel` is ever closed:

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

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. This leaks file descriptors.

### Steps to Reproduce

Run the `DumpCompactionLogCommand` tool to dump a compaction log file. After execution, the file descriptor remains open.

### What Did You Expect to See?

The `RandomAccessFile` and `FileChannel` should be properly closed after use, ideally using try-with-resources.

### What Did You See Instead?

File descriptors are leaked because neither resource is closed.

### Additional Context

The fix should use try-with-resources to ensure both the `RandomAccessFile` and `FileChannel` are properly closed.

Contributor guide

Open the contributing guide

Research direction

Start by reading DumpCompactionLogCommand.java at the RandomAccessFile and FileChannel creation shown in the issue. Run the DumpCompactionLogCommand tool against a compaction log file to understand the resource lifetime, then verify that execution closes both resources and no longer leaves the file descriptor open.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
cli
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.