-
Notifications
You must be signed in to change notification settings - Fork 12k
[Bug] Resource leak in IOTinyUtils.copyFile - FileInputStream/FileOutputStream not closed on exception #10208
Copy link
Copy link
Open
Description
Before Creating the Bug Report
- I found a bug, not just asking a question, which should be created in GitHub Discussions.
- I have searched the GitHub Issues and GitHub Discussions of this repository and believe that this is not a duplicate.
- 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
All
Describe the Bug
In IOTinyUtils.copyFile(), FileOutputStream and FileInputStream are created inline as arguments to getChannel():
tc = new FileOutputStream(tf).getChannel();
sc = new FileInputStream(sf).getChannel();The underlying stream references are lost. While closing the FileChannel typically closes the underlying stream, if an exception occurs between the two lines (e.g., FileInputStream constructor throws because the source file is locked), the already-created FileOutputStream will never be closed, leaking a file descriptor.
Additionally, this pattern makes the resource ownership unclear and is flagged by static analysis tools (SpotBugs, SonarQube).
Steps to Reproduce
- Call
IOTinyUtils.copyFile()with a valid target file and a source file that throws onFileInputStreamconstruction - The
FileOutputStream(and its channel) created on the previous line is leaked
What Did You Expect to See?
All resources should be properly closed using try-with-resources pattern.
What Did You See Instead?
Potential file descriptor leak.
Additional Context
- File:
common/src/main/java/org/apache/rocketmq/common/utils/IOTinyUtils.java:92-93 - Fix: Convert to try-with-resources pattern
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels