non-blocking SocketChannel connect broken on Windows 8

Alan Bateman Alan.Bateman at oracle.com
Thu Sep 29 07:29:49 PDT 2011


Jeroen Frijters wrote:
> Hi,
>
> While testing on Windows 8 I found a bug in SocketChannelImpl.connect() when configured non-blocking.
>
> Here's repro:
>
> import java.net.*;
> import java.nio.channels.*;
>
> public class test
> {
>   public static void main(String[] args) throws Exception
>   {
>     SocketChannel sc = SocketChannel.open();
>     sc.configureBlocking(false);
>     sc.connect(new InetSocketAddress("google.com", 12345));
>   }
> }
>
> When run on the Windows 8 Developer Preview this throws the following exception:
>
> Exception in thread "main" java.net.SocketException: Invalid argument: getsockname
> 	at sun.nio.ch.Net.localInetAddress(Native Method)
> 	at sun.nio.ch.Net.localAddress(Net.java:389)
> 	at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:639)
> 	at test.main(test.java:10)
>
> When you look at the code the problem is obvious. The connect method tries to get the localAddress while the connection is still pending, it should only do this after the connection attempt has finished.
>   
This must be behavior change in Windows 8 as I don't think we've seen 
this before (as we have several tests that do non-blocking connects).  
The usual thing when you attempt to connect is that the operating system 
should first bind to a local address (if not bound already) before it 
attempts to establish the connection. I've no doubt that this will break 
other things too.

-Alan



More information about the net-dev mailing list