InetAddressImpl#lookupAllHostAddr slow/hangs
Since I upgraded to Yosemite I’ve noticed that attempts to resolve localhost on my home network have been taking ages (sometimes over a minute) so I thought I’d try and work out why.
This is what my initial /etc/hosts file looked like based on the assumption that my machine’s hostname was teetotal:
$ cat /etc/hosts ## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost #fe80::1%lo0 localhost 127.0.0.1 wuqour.local 127.0.0.1 teetotal
I setup a little test which replicated the problem:
import java.net.InetAddress; import java.net.UnknownHostException; public class LocalhostResolution { public static void main( String[] args ) throws UnknownHostException { long start = System.currentTimeMillis(); InetAddress localHost = InetAddress.getLocalHost(); System.out.println(localHost); System.out.println(System.currentTimeMillis() - start); } }
which has the following output:
Exception in thread "main" java.net.UnknownHostException: teetotal-2: teetotal-2: nodename nor servname provided, or not known at java.net.InetAddress.getLocalHost(InetAddress.java:1473) at LocalhostResolution.main(LocalhostResolution.java:9) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) Caused by: java.net.UnknownHostException: teetotal-2: nodename nor servname provided, or not known at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:901) at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1293) at java.net.InetAddress.getLocalHost(InetAddress.java:1469) ... 6 more
Somehow my hostname has changed to teetotal-2 so I added the following entry to /etc/hosts:
127.0.0.1 teetotal-2
Now if we run the program we see this output instead:
teetotal-2/127.0.0.1 5011
It’s still taking 5 seconds to resolve which is much longer than I’d expect. After break pointing through the code it seems like it’s trying to do an IPv6 resolution rather than IPv4 so I added an /etc/hosts entry for that too:
::1 teetotal-2
Now resolution is much quicker:
teetotal-2/127.0.0.1 6
Happy days!
Reference: | InetAddressImpl#lookupAllHostAddr slow/hangs from our JCG partner Mark Needham at the Mark Needham Blog blog. |