Senrian opened a new pull request, #10228:
URL: https://github.com/apache/rocketmq/pull/10228
## Summary
Fixes issue #10218 - `DumpCompactionLogCommand.java` leaks
`RandomAccessFile` and `FileChannel` resources.
## Bug
On line 74:
```java
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:
```java
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
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]