From ada5e5507661c606fffbed4679f6fbcb4d611af9 Mon Sep 17 00:00:00 2001 From: hoteye Date: Mon, 22 Sep 2025 15:27:57 +0800 Subject: [PATCH] Fix resource leak in PortAllocator canBind method The canBind method in PortAllocator had a potential resource leak where ServerSocket instances might not be properly closed if an exception occurred between socket creation and the explicit close() call. Fixed by using try-with-resources statement which ensures the ServerSocket is automatically closed regardless of whether an exception is thrown or not. This prevents potential socket handle leaks in the testing framework. --- .../instrumentation/test/utils/PortAllocator.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/testing-common/src/main/java/io/opentelemetry/instrumentation/test/utils/PortAllocator.java b/testing-common/src/main/java/io/opentelemetry/instrumentation/test/utils/PortAllocator.java index 985f260bda83..43d29b43d10b 100644 --- a/testing-common/src/main/java/io/opentelemetry/instrumentation/test/utils/PortAllocator.java +++ b/testing-common/src/main/java/io/opentelemetry/instrumentation/test/utils/PortAllocator.java @@ -118,9 +118,7 @@ Closeable bind(int port) { } boolean canBind(int port) { - try { - ServerSocket socket = new ServerSocket(port); - socket.close(); + try (ServerSocket socket = new ServerSocket(port)) { return true; } catch (IOException exception) { return false;