Skip to content

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

@daguimu

Description

@daguimu

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

  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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions