You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by gn...@apache.org on 2026/03/19 11:58:37 UTC
(camel) branch fix-serversocket-reuseaddress created (now 60e43842cc35)
This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a change to branch fix-serversocket-reuseaddress
in repository https://gitbox.apache.org/repos/asf/camel.git
at 60e43842cc35 Fix ServerSocket port finding to use setReuseAddress(true)
This branch includes the following new commits:
new 60e43842cc35 Fix ServerSocket port finding to use setReuseAddress(true)
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(camel) 01/01: Fix ServerSocket port finding to use setReuseAddress(true)
Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a commit to branch fix-serversocket-reuseaddress
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 60e43842cc35c984ffe81b71c777f97010cfc965
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Thu Mar 19 12:58:20 2026 +0100
Fix ServerSocket port finding to use setReuseAddress(true)
Using new ServerSocket(0) to find a free port and then closing it
creates a race condition where another process can grab the port.
Fix by using setReuseAddress(true) before binding, which is the same
pattern used by AvailablePortFinder.
Co-Authored-By: Claude Opus 4.6 <no...@anthropic.com>
---
.../server/core/integ5/ServerAnnotationProcessor.java | 11 ++++++-----
.../server/core/integ5/ServerAnnotationProcessor.java | 11 ++++++-----
.../org/apache/camel/component/salesforce/PubSubApiTest.java | 7 ++-----
.../camel/test/infra/kafka/services/ConfluentContainer.java | 4 +++-
.../camel/test/infra/kafka/services/StrimziContainer.java | 4 +++-
5 files changed, 20 insertions(+), 17 deletions(-)
diff --git a/components/camel-ldap/src/test/java/org/apache/directory/server/core/integ5/ServerAnnotationProcessor.java b/components/camel-ldap/src/test/java/org/apache/directory/server/core/integ5/ServerAnnotationProcessor.java
index a3a7279948a8..016d38122adb 100644
--- a/components/camel-ldap/src/test/java/org/apache/directory/server/core/integ5/ServerAnnotationProcessor.java
+++ b/components/camel-ldap/src/test/java/org/apache/directory/server/core/integ5/ServerAnnotationProcessor.java
@@ -19,6 +19,7 @@ package org.apache.directory.server.core.integ5;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
+import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.Arrays;
@@ -350,11 +351,11 @@ public final class ServerAnnotationProcessor {
}
private static int getFreePort() throws IOException {
- ServerSocket ss = new ServerSocket(0);
- int port = ss.getLocalPort();
- ss.close();
-
- return port;
+ try (ServerSocket ss = new ServerSocket()) {
+ ss.setReuseAddress(true);
+ ss.bind(new InetSocketAddress(0), 1);
+ return ss.getLocalPort();
+ }
}
}
diff --git a/components/camel-ldif/src/test/java/org/apache/directory/server/core/integ5/ServerAnnotationProcessor.java b/components/camel-ldif/src/test/java/org/apache/directory/server/core/integ5/ServerAnnotationProcessor.java
index a3a7279948a8..016d38122adb 100644
--- a/components/camel-ldif/src/test/java/org/apache/directory/server/core/integ5/ServerAnnotationProcessor.java
+++ b/components/camel-ldif/src/test/java/org/apache/directory/server/core/integ5/ServerAnnotationProcessor.java
@@ -19,6 +19,7 @@ package org.apache.directory.server.core.integ5;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
+import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.Arrays;
@@ -350,11 +351,11 @@ public final class ServerAnnotationProcessor {
}
private static int getFreePort() throws IOException {
- ServerSocket ss = new ServerSocket(0);
- int port = ss.getLocalPort();
- ss.close();
-
- return port;
+ try (ServerSocket ss = new ServerSocket()) {
+ ss.setReuseAddress(true);
+ ss.bind(new InetSocketAddress(0), 1);
+ return ss.getLocalPort();
+ }
}
}
diff --git a/components/camel-salesforce/camel-salesforce-component/src/test/java/org/apache/camel/component/salesforce/PubSubApiTest.java b/components/camel-salesforce/camel-salesforce-component/src/test/java/org/apache/camel/component/salesforce/PubSubApiTest.java
index c559c3600a82..a828f85cd4b3 100644
--- a/components/camel-salesforce/camel-salesforce-component/src/test/java/org/apache/camel/component/salesforce/PubSubApiTest.java
+++ b/components/camel-salesforce/camel-salesforce-component/src/test/java/org/apache/camel/component/salesforce/PubSubApiTest.java
@@ -17,7 +17,6 @@
package org.apache.camel.component.salesforce;
import java.io.IOException;
-import java.net.ServerSocket;
import java.util.Base64;
import com.google.protobuf.ByteString;
@@ -246,9 +245,7 @@ public class PubSubApiTest {
}
}
- private int getPort() throws IOException {
- try (ServerSocket serverSocket = new ServerSocket(0)) {
- return serverSocket.getLocalPort();
- }
+ private int getPort() {
+ return org.apache.camel.test.AvailablePort.probePort(null, 0);
}
}
diff --git a/test-infra/camel-test-infra-kafka/src/main/java/org/apache/camel/test/infra/kafka/services/ConfluentContainer.java b/test-infra/camel-test-infra-kafka/src/main/java/org/apache/camel/test/infra/kafka/services/ConfluentContainer.java
index 37da65ecad66..e1b2a981c113 100644
--- a/test-infra/camel-test-infra-kafka/src/main/java/org/apache/camel/test/infra/kafka/services/ConfluentContainer.java
+++ b/test-infra/camel-test-infra-kafka/src/main/java/org/apache/camel/test/infra/kafka/services/ConfluentContainer.java
@@ -105,7 +105,9 @@ public class ConfluentContainer extends GenericContainer<ConfluentContainer> {
}
private static int findFreePort() {
- try (ServerSocket socket = new ServerSocket(0)) {
+ try (ServerSocket socket = new ServerSocket()) {
+ socket.setReuseAddress(true);
+ socket.bind(new java.net.InetSocketAddress((java.net.InetAddress) null, 0), 1);
return socket.getLocalPort();
} catch (IOException e) {
throw new RuntimeException("Failed to find a free port", e);
diff --git a/test-infra/camel-test-infra-kafka/src/main/java/org/apache/camel/test/infra/kafka/services/StrimziContainer.java b/test-infra/camel-test-infra-kafka/src/main/java/org/apache/camel/test/infra/kafka/services/StrimziContainer.java
index 6d39216fbbc6..e787f2812fb7 100644
--- a/test-infra/camel-test-infra-kafka/src/main/java/org/apache/camel/test/infra/kafka/services/StrimziContainer.java
+++ b/test-infra/camel-test-infra-kafka/src/main/java/org/apache/camel/test/infra/kafka/services/StrimziContainer.java
@@ -104,7 +104,9 @@ public class StrimziContainer extends GenericContainer<StrimziContainer> {
}
private static int findFreePort() {
- try (ServerSocket socket = new ServerSocket(0)) {
+ try (ServerSocket socket = new ServerSocket()) {
+ socket.setReuseAddress(true);
+ socket.bind(new java.net.InetSocketAddress((java.net.InetAddress) null, 0), 1);
return socket.getLocalPort();
} catch (IOException e) {
throw new RuntimeException("Failed to find a free port", e);