apache / apache/rocketmq

[Bug] Resource leak in IOTinyUtils.copyFile - FileInputStream/FileOutputStream not closed on exception

Open Beginner friendly
#10,208 3 comments 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

All

### Describe the Bug

In `IOTinyUtils.copyFile()`, `FileOutputStream` and `FileInputStream` are created inline as arguments to `getChannel()`:

```java
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

1. Call `IOTinyUtils.copyFile()` with a valid target file and a source file that throws on `FileInputStream` construction
2. 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

Contributor guide

Open the contributing guide

Research direction

Start in common/src/main/java/org/apache/rocketmq/common/utils/IOTinyUtils.java at lines 92-93 and read the copyFile() resource-handling path. Reproduce or inspect the failure between opening the target and source, then verify with the project's existing tests that all streams and channels are closed when construction or copying throws.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.