Hi Endre,

I wrote a small test program that creates a socket channel, closes it and 
creates another one using the same port:

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.StandardSocketOptions;
import java.nio.channels.NetworkChannel;
import java.nio.channels.ServerSocketChannel;

public class Main {

    public static void main(String[] args) throws IOException, 
InterruptedException {
        int port = Integer.parseInt(args[0]);
        SocketAddress addr = new InetSocketAddress(port);
        System.out.println("Binding server socket to " + addr);
        ServerSocketChannel s = ServerSocketChannel.open();
        s.setOption(StandardSocketOptions.SO_REUSEADDR, true);
        System.out.println("Created server socket channel " + s);
        NetworkChannel c = s.bind(addr);
        System.out.println("Bound server socket to " + addr);
        s.close();
        System.out.println("Closed channel: " + s);
        Thread.sleep(1000);
        System.out.println("Binding server socket to " + addr);
        ServerSocketChannel s2 = ServerSocketChannel.open();
        s2.setOption(StandardSocketOptions.SO_REUSEADDR, true);
        System.out.println("Created server socket channel " + s2);
        NetworkChannel c2 = s2.bind(addr);
        System.out.println("Bound server socket to " + addr);
        s2.close();
        System.out.println("Closed channel: " + s2);

    }
}

It works perfectly both on Linux and on Windows. Here is the output on Windows:

C:\Users\Michael\Desktop>java Main 8888
Binding server socket to 0.0.0.0/0.0.0.0:8888
Created server socket channel sun.nio.ch.ServerSocketChannelImpl[unbound]
Bound server socket to 0.0.0.0/0.0.0.0:8888
Closed channel: sun.nio.ch.ServerSocketChannelImpl[closed]
Binding server socket to 0.0.0.0/0.0.0.0:8888
Created server socket channel sun.nio.ch.ServerSocketChannelImpl[unbound]
Bound server socket to 0.0.0.0/0.0.0.0:8888
Closed channel: sun.nio.ch.ServerSocketChannelImpl[closed]


There is also AsynchronousServerSocketChannel, but I doubt this makes a big 
difference. I really don't think that NIO does not properly unbind sockets 
on Windows. There might be a small delay, but certainly not 10 Minutes! As 
far as I know StandardSocketOptions.SO_REUSEADDR should allow to reuse 
sockets that are no longer in use, even if the operating system did not 
unbind them yet.

If there is anything I can do to help, let me know.

Best regards,
Michael

On Tuesday, April 5, 2016 at 5:08:09 PM UTC+2, drewhk wrote:
>
>
>
> On Tue, Apr 5, 2016 at 5:04 PM, Michi <michael...@physik.tu-muenchen.de 
> <javascript:>> wrote:
>
>> Hi Endre,
>>
>> even after 10 Minutes TCPView shows that the socket is still bound by 
>> jawaw.exe. If I close the application, the socket gets unbound immediately. 
>> That's why I am wondering if I do something wrong or if this is a bug in 
>> Akka HTTP. 
>>
>
> We use NIO underneath and don't do anything special, so I am not sure what 
> we can do here (it works on all other OSes).
>  
>
>>
>> I tried to use a independent Actor System for the Http Server class and 
>> shut the system down after unbinding, but that does not help either.
>>
>
> At that point Akka had cleaned up every actor, so I am not sure what else 
> to do :(
>  
>
>>
>> Unfortunately our customer uses Windows and I am wondering if there is 
>> anything I can do about it or if it would be better to reimplement the code 
>> with Apache Http Components.
>>
>
> You can try out other frameworks, if they happen to work (and use NIO) 
> then at least we can see what magic they do to make Windows happy :(
>
> -Endre
>  
>
>>
>> best regards,
>> Michael
>>
>> On Tuesday, April 5, 2016 at 2:07:52 PM UTC+2, drewhk wrote:
>>>
>>> Hi,
>>>
>>> As far as I know event though NIO reports the unbind, unbinding on 
>>> Windows is not immediate and you are not guaranteed to be able to bind 
>>> again immediately. I don't know if this comes from Windows or the NIO 
>>> windows drivers though.
>>>
>>> -Endre
>>>
>>> On Tue, Apr 5, 2016 at 2:05 PM, Michi <michael...@physik.tu-muenchen.de> 
>>> wrote:
>>>
>>>> Hi,
>>>>
>>>> we have a problem with Akka HTTP on Windows: after creating a server 
>>>> binding and unbinding it, it is not possible to bind it again. We get a 
>>>> akka.stream.BindFailedException. On Linux, this works without problems.
>>>>
>>>> The application uses the following code to create the server binding:
>>>>
>>>>   private val binding: Future[ServerBinding] = {
>>>>     val httpsContext = if (protocol == Protocol.Https) 
>>>> Some(HttpsContexts.serverContext) else None
>>>>     val serverSource: Source[Http.IncomingConnection, 
>>>> Future[Http.ServerBinding]] = Http(actorSystem).bind(interface = 
>>>> interface, 
>>>> port = port, httpsContext = httpsContext)
>>>>
>>>>     logger.info("Binding to interface " + interface + ":" + port + " 
>>>> using " + protocol)
>>>>
>>>>     val bindingFuture: Future[ServerBinding] = 
>>>> serverSource.to(Sink.foreach { connection =>
>>>>       connection.handleWith(route)
>>>>     }).run()
>>>>     bindingFuture
>>>>   }
>>>>
>>>>   binding onComplete {
>>>>     case Success(result) => logger.info("Bound to interface " + 
>>>> interface + ":" + port + " using " + protocol)
>>>>     case Failure(reason) => logger.error("Could not bind to interface " 
>>>> + interface + ":" + port + " using " + protocol, reason)
>>>>   }
>>>>
>>>> protected override def dispose(): Unit = {
>>>>     // stop webserver
>>>>     for(binding <- binding) {
>>>>       binding.unbind() onComplete {
>>>>         case Success(result) => logger.info("Unbound " + binding)
>>>>         case Failure(reason) => logger.error("Could not unbind " + 
>>>> binding, reason)
>>>>       }
>>>>     }
>>>>   }
>>>>
>>>> Restarting the application solves the problem.
>>>>
>>>> Is there anything I can do to solve the problem?
>>>>
>>>> Thanks,
>>>> Michael
>>>>
>>>> -- 
>>>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>>>> >>>>>>>>>> Check the FAQ: 
>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>> >>>>>>>>>> Search the archives: 
>>>> https://groups.google.com/group/akka-user
>>>> --- 
>>>> You received this message because you are subscribed to the Google 
>>>> Groups "Akka User List" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send 
>>>> an email to akka-user+...@googlegroups.com.
>>>> To post to this group, send email to akka...@googlegroups.com.
>>>> Visit this group at https://groups.google.com/group/akka-user.
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>
>>> -- 
>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>> >>>>>>>>>> Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>> >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
>> --- 
>> You received this message because you are subscribed to the Google Groups 
>> "Akka User List" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to akka-user+...@googlegroups.com <javascript:>.
>> To post to this group, send email to akka...@googlegroups.com 
>> <javascript:>.
>> Visit this group at https://groups.google.com/group/akka-user.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to