RLDE26 opened a new issue, #11602:
URL: https://github.com/apache/rocketmq/issues/11602

   ### Before Creating the Enhancement Request
   
   - [x] I have confirmed that this should be classified as an enhancement 
rather than a bug/feature.
   
   
   ### Summary
   
   **1. 
`org.apache.rocketmq.controller.ControllerStartup#createControllerManager` — 
`InputStream in`**
   
   <img width="803" height="242" alt="image" 
src="https://github.com/user-attachments/assets/ff07e8e7-2b58-400c-9a6b-74f3d96cbd1f";
 />
   
   `ControllerStartup.java:87`, closed on line 96. If `properties.load(in)` 
(line 89) or any of the four `MixAll.properties2Object` calls (lines 90-93) 
throws, `in.close()` is skipped.
   
   **2. 
`org.apache.rocketmq.namesrv.NamesrvStartup#parseCommandlineAndConfigFile` — 
`InputStream in`**
   
   <img width="1092" height="577" alt="image" 
src="https://github.com/user-attachments/assets/fd7b575c-43e4-455f-b7ff-67902153e27b";
 />
   
   `NamesrvStartup.java:101`, closed on line 117. Same shape as 1.
   
   **3. `org.apache.rocketmq.common.config.AbstractRocksDBStorage#shutdown` —  
releases share a single `try` block**
   
   `AbstractRocksDBStorage.java:509-599`. All releases run inside one `try`, 
and the `catch` block logs the exception and returns `false`. Several of the 
calls in that sequence can throw, and when one of them does, every release 
after it is skipped — including `db.closeE()` (572) and `this.loaded = false` 
(592). The `RocksDB` instance, its options, its column family handles and the 
LOCK file of the DB directory then remain open until the process exits.
   
   **4. `org.apache.rocketmq.tools.command.metadata.RocksDBConfigToJsonCommand` 
— `RocksIterator` × 2**
   
   `getConfigMapFromRocksDB:203` and `loadConsumerOffsets:306`. Neither 
iterator is ever closed, and in both methods the `finally` (239, 325) shuts the 
DB down while the iterator is still open and still points into it. Native 
memory is not reclaimed: `AbstractNativeReference` documents that `close()` "is 
no longer called automatically during the regular Java GC process via 
finalization".
   
   **5. `org.apache.rocketmq.remoting.netty.TlsHelper#buildSslContext` — 
`FileInputStream` × 2**
   
   <img width="1113" height="121" alt="image" 
src="https://github.com/user-attachments/assets/c439c563-3dfe-4950-85b3-25a83484a25d";
 />
   
   <img width="1111" height="142" alt="image" 
src="https://github.com/user-attachments/assets/b24785f5-5ed0-43c9-b5b7-de588bef33e3";
 />
   
   `TlsHelper.java:130` and `:144`. Both streams are passed inline to 
`SslContextBuilder#keyManager(InputStream, InputStream, String)`, which reads 
them but does not close them (verified against netty-handler 4.1.130), and the 
caller keeps no reference. It runs on the certificate-change callback, so every 
rotation leaks two fds. The same file already does it right in 
`extractTlsConfigFromFile` (177-194).
   
   **6. `org.apache.rocketmq.common.compression.ZstdCompressor#compress` — 
`ZstdOutputStream outputStream`**
   
   <img width="1132" height="667" alt="image" 
src="https://github.com/user-attachments/assets/6465f8b1-67e5-4a88-979b-0b5f4b792eb4";
 />
   
   `ZstdCompressor.java:36`. If an exception occurs during 
`outputStream.write(src)` or `outputStream.flush()`, the `outputStream.close()` 
line will be skipped, and the `ZstdOutputStream` will not be closed. The 
`finally` block only closes the underlying `ByteArrayOutputStream`, which 
cannot replace closing the wrapper stream `ZstdOutputStream`.
   
   
   ### Motivation
   
   This enhancement effort revealed potential resource leaks. Recurrent leakage 
would cause an aggregation of unreleased resources, ultimately leading to the 
depletion of system resources available to the application.
   
   ### Describe the Solution You'd Like
   
   - 1, 2, 4, 5, 6: `try-with-resources`. For 4 the iterator has to be closed 
**before** the DB is shut down, which `try-with-resources` around the iterator 
with `shutdown` in the outer `finally` gives.
   - 3: wrap the calls in the release sequence that can throw, so that a 
failure in one does not abort the rest and `db.closeE()` is still reached.
   
   ### Describe Alternatives You've Considered
   
   - 3 could be addressed with `finally { this.loaded = false; }` alone. That 
fixes the state, but every native resource still stays open.
   - 3 could also split the whole body into one `try` per resource. That covers 
more failures, but it rewrites a 90-line method; wrapping only the calls that 
can throw keeps the diff small.
   - 1, 2, 5 could use an explicit `try`/`finally` instead of 
`try-with-resources`; `try-with-resources` is shorter and also covers an 
exception thrown by `close()` itself.
   
   ### Additional Context
   
   _No response_


-- 
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]

Reply via email to