You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Emmanuel Lecharny <el...@gmail.com> on 2010/01/14 13:27:09 UTC

Pb on OSX with port availablity discovering

Hi guys,

this is a problem I'm having for days now, and it's pretty painfull. It 
occurs on Mac OSX only (leopard), Java 1.6.0_17.

When running mvn -Pserial package assembly:assembly, tests are run twice 
(no clue about it, probably something wrong in Maven...), and for a test 
(NioFileRegionTest), we get an exception :

Tests in error:
  testSendLargeFile(org.apache.mina.transport.socket.nio.NioFileRegionTest)
  
testSessionCallbackInvocation(org.apache.mina.transport.socket.nio.SocketConnectorTest)


-------------------------------------------------------------------------------
Test set: org.apache.mina.transport.socket.nio.NioFileRegionTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.051 
sec <<< FAILURE!
testSendLargeFile(org.apache.mina.transport.socket.nio.NioFileRegionTest)  
Time elapsed: 0.044 sec  <<< ERROR!
java.net.BindException: Address already in use
        at sun.nio.ch.Net.bind(Native Method)
        at 
sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:119)
        at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:59)
        at 
org.apache.mina.transport.socket.nio.NioSocketAcceptor.open(NioSocketAcceptor.java:251)
        at 
org.apache.mina.transport.socket.nio.NioSocketAcceptor.open(NioSocketAcceptor.java:48)
        at 
org.apache.mina.core.polling.AbstractPollingIoAcceptor.registerHandles(AbstractPollingIoAcceptor.java:523)
        at 
org.apache.mina.core.polling.AbstractPollingIoAcceptor.access$200(AbstractPollingIoAcceptor.java:65)
        at 
org.apache.mina.core.polling.AbstractPollingIoAcceptor$Acceptor.run(AbstractPollingIoAcceptor.java:407)
        at 
org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:64)
        at 
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
        at 
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
        at java.lang.Thread.run(Thread.java:637)

I added a hell of traces. What's going on is that the test is started, 
it tries to grab an available port using 
AvailablePortFinder.getNextAvailable(1025). This method finds that the 
port 1025 is available, returns it to the method, which tries to bind an 
acceptor n it, and obtains the BindException : Address already in use.

Very wrong... I have absolutely no clue about what can be the cause of 
this problem, assuming that it works perfectly well on Linux and on Windows.

What is strange is that running mvn -Pserial clean install works well 
(but then the test is run only once). Also it always fail on the same tests.

I have added some missing 'connector/acceptor.dispose()', to no rescue. 
It seems like the only way to get it running is to pick a dfferent port 
for each test (1024, then 2025, ...)

-- 
Regards,
Cordialement,
Emmanuel Lécharny
www.nextury.com



Re: Pb on OSX with port availablity discovering

Posted by Emmanuel Lecharny <el...@gmail.com>.
Emmanuel LŽcharny a écrit :
> Finally got it working fine. I was in a hurry when I posted my last mail, but 
> the error I got were due to a ClassCastException (my bad).
>
> Now, it's back on line and all tests are passing fine. Your fix is correct.
>   


Re: Pb on OSX with port availablity discovering

Posted by Niklas Gustavsson <ni...@protocol7.com>.
On Thu, Jan 14, 2010 at 1:27 PM, Emmanuel Lecharny <el...@gmail.com> wrote:
> java.net.BindException: Address already in use

The problem is due to running the test twice within the timeout for
the socket TIME_WAIT period. Unsure if this might be longer on OS X,
causing your problems (or your build might be faster).

I'm unsure why AvailablePortFinder do not detect that the port is in
use (possible because it enables reuse). However, here are two
different patches which both fixes the problem (albeit in different
ways). The first lets the acceptor pick a free port. The second
enables reuse of the acceptor port.

/niklas

Index: src/test/java/org/apache/mina/transport/AbstractFileRegionTest.java
===================================================================
--- src/test/java/org/apache/mina/transport/AbstractFileRegionTest.java	(revision
899136)
+++ src/test/java/org/apache/mina/transport/AbstractFileRegionTest.java	(working
copy)
@@ -60,7 +60,6 @@
         final boolean[] success = {false};
         final Throwable[] exception = {null};

-        int port = AvailablePortFinder.getNextAvailable(1025);
         IoAcceptor acceptor = createAcceptor();

         acceptor.setHandler(new IoHandlerAdapter() {
@@ -91,7 +90,8 @@
             }
         });

-        acceptor.bind(new InetSocketAddress(port));
+        acceptor.bind(new InetSocketAddress(0));
+        int port = ((InetSocketAddress)acceptor.getLocalAddress()).getPort();

         IoConnector connector = createConnector();
         connector.setHandler(new IoHandlerAdapter() {
@@ -124,6 +124,7 @@
         assertEquals("Written bytes should match file size",
FILE_SIZE, session.getWrittenBytes());

         connector.dispose();
+        acceptor.unbind();
         acceptor.dispose();
     }


============================================

Index: src/test/java/org/apache/mina/transport/socket/nio/NioFileRegionTest.java
===================================================================
--- src/test/java/org/apache/mina/transport/socket/nio/NioFileRegionTest.java	(revision
899136)
+++ src/test/java/org/apache/mina/transport/socket/nio/NioFileRegionTest.java	(working
copy)
@@ -32,7 +32,10 @@

     @Override
     protected IoAcceptor createAcceptor() {
-        return new NioSocketAcceptor();
+    	NioSocketAcceptor acceptor = new NioSocketAcceptor();
+    	// make sure to allow for reusing the address to allow repeated test runs
+    	acceptor.setReuseAddress(true);
+        return acceptor;
     }

     @Override