You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ti...@apache.org on 2021/04/15 20:42:07 UTC

[maven-surefire] 01/01: java.net.BindException: Cannot assign requested address

This is an automated email from the ASF dual-hosted git repository.

tibordigana pushed a commit to branch gh-ci
in repository https://gitbox.apache.org/repos/asf/maven-surefire.git

commit 85eaf430128bb753256f5764ca029213b30e8f1b
Author: tibordigana <ti...@gmail.com>
AuthorDate: Thu Apr 15 22:11:50 2021 +0200

    java.net.BindException: Cannot assign requested address
---
 .../surefire/extensions/SurefireForkChannel.java   | 35 ++++++++++++++++++++--
 .../api/util/internal/AsyncSocketTest.java         |  6 ++--
 2 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/SurefireForkChannel.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/SurefireForkChannel.java
index 640d562..45f262c 100644
--- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/SurefireForkChannel.java
+++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/SurefireForkChannel.java
@@ -32,14 +32,17 @@ import org.apache.maven.surefire.extensions.util.LineConsumerThread;
 import javax.annotation.Nonnull;
 import java.io.Closeable;
 import java.io.IOException;
+import java.net.BindException;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.SocketOption;
 import java.nio.Buffer;
 import java.nio.ByteBuffer;
+import java.nio.channels.AlreadyBoundException;
 import java.nio.channels.AsynchronousServerSocketChannel;
 import java.nio.channels.AsynchronousSocketChannel;
 import java.nio.channels.ReadableByteChannel;
+import java.nio.channels.UnsupportedAddressTypeException;
 import java.nio.channels.WritableByteChannel;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
@@ -87,14 +90,40 @@ final class SurefireForkChannel extends ForkChannel
         super( arguments );
         server = open( withThreadPool( THREAD_POOL ) );
         setTrueOptions( SO_REUSEADDR, TCP_NODELAY, SO_KEEPALIVE );
-        InetAddress ip = InetAddress.getLoopbackAddress();
-        server.bind( new InetSocketAddress( ip, 0 ), 1 );
-        InetSocketAddress localAddress = (InetSocketAddress) server.getLocalAddress();
+        InetSocketAddress localAddress = bindSocketAddress();
         localHost = localAddress.getHostString();
         localPort = localAddress.getPort();
         sessionId = arguments.getSessionId();
     }
 
+    private InetSocketAddress bindSocketAddress() throws IOException
+    {
+        InetSocketAddress localLoopback = findLocalAddress(InetAddress.getLoopbackAddress()  );
+        if ( localLoopback != null )
+        {
+            return localLoopback;
+        }
+        InetSocketAddress localhost = findLocalAddress( InetAddress.getLocalHost() );
+        if ( localhost != null )
+        {
+            return localhost;
+        }
+        throw new IOException( "Could not find local IP address." );
+    }
+
+    private InetSocketAddress findLocalAddress( InetAddress ip ) throws IOException
+    {
+        try
+        {
+            server.bind( new InetSocketAddress( ip, 0 ), 1 );
+            return (InetSocketAddress) server.getLocalAddress();
+        }
+        catch ( BindException | AlreadyBoundException | UnsupportedAddressTypeException e )
+        {
+            return null;
+        }
+    }
+
     @Override
     public void connectToClient() throws IOException
     {
diff --git a/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/AsyncSocketTest.java b/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/AsyncSocketTest.java
index f4cb773..afadc9f 100644
--- a/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/AsyncSocketTest.java
+++ b/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/AsyncSocketTest.java
@@ -65,7 +65,7 @@ public class AsyncSocketTest
 
     private volatile InetSocketAddress address;
 
-    @Test
+    @Test( timeout = 10_000L )
     public void test() throws Exception
     {
         int forks = 2;
@@ -80,7 +80,7 @@ public class AsyncSocketTest
         AsynchronousChannelGroup group = AsynchronousChannelGroup.withThreadPool( executorService );
         AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel.open( group );
         setTrueOptions( server, SO_REUSEADDR, TCP_NODELAY, SO_KEEPALIVE );
-        InetAddress ip = InetAddress.getLocalHost();
+        InetAddress ip = InetAddress.getLoopbackAddress();
         server.bind( new InetSocketAddress( ip, 0 ), 1 );
         address = (InetSocketAddress) server.getLocalAddress();
 
@@ -192,7 +192,7 @@ public class AsyncSocketTest
     @SuppressWarnings( "checkstyle:magicnumber" )
     private void client() throws Exception
     {
-        InetSocketAddress hostAddress = new InetSocketAddress( InetAddress.getLocalHost(), address.getPort() );
+        InetSocketAddress hostAddress = new InetSocketAddress( InetAddress.getLoopbackAddress(), address.getPort() );
         AsynchronousSocketChannel clientSocketChannel = AsynchronousSocketChannel.open();
         clientSocketChannel.connect( hostAddress ).get(); // Wait until connection is done.
         InputStream is = new BufferedInputStream( newInputStream( clientSocketChannel ), 64 * 1024 );