Chengxin Xu created ZOOKEEPER-4935: -------------------------------------- Summary: Fix potential resource leak Key: ZOOKEEPER-4935 URL: https://issues.apache.org/jira/browse/ZOOKEEPER-4935 Project: ZooKeeper Issue Type: Improvement Components: server Affects Versions: 3.9.3 Reporter: Chengxin Xu Fix For: 3.9.3 Attachments: image-2025-06-04-13-13-28-697.png, image-2025-06-04-13-14-27-680.png, image-2025-06-04-13-26-06-474.png, image-2025-06-04-13-30-56-580.png
zookeeper-server/src/main/java/org/apache/zookeeper/server/controller/ControllerServerConfig.java#ensureComplete Any exception occurring before both {{close()}} calls complete successfully can lead to one or both {{ServerSocket}} instances (and the system ports they hold) not being released. The best way to fix this is to use the {{try-with-resources}} statement, which guarantees that {{Closeable}} resources are closed, even if exceptions occur. Besides, it seems there are some other leaks I found later, which not methoned in github issue: leak2: org.apache.zookeeper.server.quorum.Learner#validateSession DataOutputStream dos if an exception occurs before dos.close(); is reached, dos.close() will be skipped. !image-2025-06-04-13-13-28-697.png! leak3: org.apache.zookeeper.server.quorum.Learner#request DataOutputStream oa if an exception occurs before oa.close(); is reached, oa.close() will be skipped. !image-2025-06-04-13-14-27-680.png! leak 4 org.apache.zookeeper.server.SnapshotComparer#getSnapshot The code opens snapIS to read from the snapshot file but never explicitly calls snapIS.close(). !image-2025-06-04-13-26-06-474.png! leak 5 org.apache.zookeeper.server.TraceFormatter#main FileInputStream、FileChannel !image-2025-06-04-13-30-56-580.png! They needs to be explicitly released by calling their close() method. -- This message was sent by Atlassian Jira (v8.20.10#820010)