jdk opening connection to self on socket creation

Steven Hartland killing at multiplay.co.uk
Wed Jun 8 20:51:27 UTC 2011


Ok I've found the code thats causing the problem its the static
initialiser for PlainSocketImpl which calls getMarkerFD that
specifically creates a connection to itself on loopback.

/*
 * Create the marker file descriptor by establishing a loopback connection
 * which we shutdown but do not close the fd. The result is an fd that
 * can be used for read/write.
 */

This explicitly creates a socket but doesn't close it as
you can see from the comment. This means you end up with
a socket in TIME_WAIT, which in our case was preventing
timely restart of any jails that run java processes that
create sockets.

A workaround could be to use the no local time wait sysctl
but I don't know what other effects this could have.
net.inet.tcp.nolocaltimewait=1

Alternatively replacing this code with the version now
included in jdk7, which uses unix domain sockets instead,
is what we're using and can confirm fixes the issues with
jail shutdowns

The patch can be found here:-
http://hg.openjdk.java.net/jdk7/build/jdk/rev/7c9d632e7323

    Regards
    Steve

----- Original Message ----- 
From: "Steven Hartland" <killing at multiplay.co.uk>
To: <freebsd-java at freebsd.org>
Sent: Wednesday, June 08, 2011 7:50 PM
Subject: jdk opening connection to self on socket creation


> I'm trying to find a bug which looks like its in the jdk
> where by when you create a ServerSocket in java it opens
> a connection to itself on loopback prior to creating
> the requested socket.
>
> This wouldn't be an issue if it was cleanly shutting dow
> said socket but it isn't. This is preventing jail clean
> shutdown in a timely manor as the socket is left in TIME_WAIT.
>
> The following code causes this behviour:-
>
> import java.net.InetAddress;
> import java.net.ServerSocket;
>
> public class Main {
>    public static void main(String[] args) throws java.io.IOException {
>        InetAddress inetaddress = InetAddress.getByName( "10.10.0.21" );
>        ServerSocket d = new ServerSocket(25665, 0, inetaddress);
>        System.out.println( "Done" );
>    }
> }
>
> If you then perform a netstat -na | grep 127.0.0.1 you
> will see the offending socket e.g.
> tcp4       0      0 127.0.0.1.26948        127.0.0.1.46429        TIME_WAIT
>
> This looks like it might be caused by libnet.so in the jre
> being loaded bi SocketAddres?
>
> write(1,"[Loaded java.net.SocksSocketImpl"...,74) = 74 (0x4a)
> stat("/usr/local/openjdk6/jre/lib/amd64/libnet.so",{ mode=-rwxr-xr-x ,inode=76507,size=98571,blksize=98816 }) = 0 (0x0)
> socket(PF_INET,SOCK_STREAM,0)            = 5 (0x5)
> listen(0x5,0x1,0x0,0x1a,0x2,0x80535a8b8)     = 0 (0x0)
> getsockname(5,{ AF_INET 0.0.0.0:12095 },0x7fffffbfe37c) = 0 (0x0)
> socket(PF_INET,SOCK_STREAM,0)            = 6 (0x6)
> connect(6,{ AF_INET 127.0.0.1:12095 },16)    = 0 (0x0)
> fcntl(5,F_GETFL,)                = 2 (0x2)
> fcntl(5,F_SETFL,O_NONBLOCK|0x2)          = 0 (0x0)
> accept(5,{ AF_INET 127.0.0.1:38895 },0x7fffffbfe37c) = 7 (0x7)
> shutdown(6,SHUT_RDWR)                = 0 (0x0)
> close(7)                     = 0 (0x0)
> close(5)                     = 0 (0x0)
> lseek(3,0x2cce5e0,SEEK_SET)          = 46982624 (0x2cce5e0)
> read(3,"PK\^C\^D\n\0\0\0\0\0I\M^W{>\M-/"...,30)  = 30 (0x1e)
> lseek(3,0x2cce61e,SEEK_SET)          = 46982686 (0x2cce61e)
> read(3,"\M-J\M-~\M-:\M->\0\0\0001\0z\n\0"...,3028) = 3028 (0xbd4)
> lseek(3,0x2cce4b9,SEEK_SET)          = 46982329 (0x2cce4b9)
> read(3,"PK\^C\^D\n\0\0\0\0\0G\M^W{>K0"...,30)    = 30 (0x1e)
> lseek(3,0x2cce4f3,SEEK_SET)          = 46982387 (0x2cce4f3)
> read(3,"\M-J\M-~\M-:\M->\0\0\0001\0\^O\n"...,237) = 237 (0xed)
> write(1,"[Loaded java.net.SocketAddress f"...,72) = 72 (0x48)
>
> Any ideas?
>
>    Regards
>    Steve
>
>
> ================================================
> This e.mail is private and confidential between Multiplay (UK) Ltd. and the person or entity to whom it is addressed. In the 
> event of misdirection, the recipient is prohibited from using, copying, printing or otherwise disseminating it or any 
> information contained in it.
> In the event of misdirection, illegible or incomplete transmission please telephone +44 845 868 1337
> or return the E.mail to postmaster at multiplay.co.uk.
>
> _______________________________________________
> freebsd-java at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-java
> To unsubscribe, send any mail to "freebsd-java-unsubscribe at freebsd.org"
> 


================================================
This e.mail is private and confidential between Multiplay (UK) Ltd. and the person or entity to whom it is addressed. In the event of misdirection, the recipient is prohibited from using, copying, printing or otherwise disseminating it or any information contained in it. 

In the event of misdirection, illegible or incomplete transmission please telephone +44 845 868 1337
or return the E.mail to postmaster at multiplay.co.uk.



More information about the freebsd-java mailing list