You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by "Senrian (via GitHub)" <gi...@apache.org> on 2026/03/29 10:17:57 UTC

[PR] [ISSUE #10208] Fix resource leak in IOTinyUtils.copyFile - use try-with-resources [rocketmq]

Senrian opened a new pull request, #10227:
URL: https://github.com/apache/rocketmq/pull/10227

   ## Summary
   
   Fixes issue #10208 - `IOTinyUtils.copyFile()` has a resource leak bug where if the `FileInputStream` constructor throws an exception after the `FileOutputStream` has been created, the `FileOutputStream` and its underlying file descriptor are leaked.
   
   ## Changes
   
   Convert the `copyFile` method from manual try-finally resource management to try-with-resources pattern:
   
   **Before (buggy):**
   ```java
   FileChannel sc = null;
   FileChannel tc = null;
   try {
       tc = new FileOutputStream(tf).getChannel();  // ← If next line throws, this leaks
       sc = new FileInputStream(sf).getChannel();
       sc.transferTo(0, sc.size(), tc);
   } finally {
       if (null != sc) { sc.close(); }
       if (null != tc) { tc.close(); }
   }
   ```
   
   **After (fixed):**
   ```java
   try (FileInputStream fis = new FileInputStream(sf);
        FileOutputStream fos = new FileOutputStream(tf);
        FileChannel sc = fis.getChannel();
        FileChannel tc = fos.getChannel()) {
       sc.transferTo(0, sc.size(), tc);
   }
   ```
   
   ## Why This Matters
   
   1. **Resource safety**: If `FileInputStream` constructor throws (e.g., source file is locked), the `FileOutputStream` created on the previous line would leak
   2. **Static analysis**: This pattern is flagged by SpotBugs, SonarQube, and other tools
   3. **Modern Java**: Try-with-resources is the idiomatic pattern since Java 7
   
   ## Testing
   
   The existing unit tests should pass as the functional behavior is unchanged. The fix only improves resource management.
   
   Fixes #10208


-- 
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: commits-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [ISSUE #10208] Fix resource leak in IOTinyUtils.copyFile - use try-with-resources [rocketmq]

Posted by "Senrian (via GitHub)" <gi...@apache.org>.
Senrian closed pull request #10227: [ISSUE #10208] Fix resource leak in IOTinyUtils.copyFile - use try-with-resources
URL: https://github.com/apache/rocketmq/pull/10227


-- 
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: commits-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [ISSUE #10208] Fix resource leak in IOTinyUtils.copyFile - use try-with-resources [rocketmq]

Posted by "Senrian (via GitHub)" <gi...@apache.org>.
Senrian commented on PR #10227:
URL: https://github.com/apache/rocketmq/pull/10227#issuecomment-4150254377

   ## PR Status Note
   
   This PR fixes a resource leak bug in `IOTinyUtils.copyFile()` - converting manual try-finally to try-with-resources (4 additions, 12 deletions).
   
   Note: CI does not run automatically on fork PRs due to GitHub security policy. Could a maintainer please trigger CI or approve for merge?
   
   The fix is minimal and functional behavior is unchanged.


-- 
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: commits-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org