{"id":2015,"date":"2016-10-12T06:34:37","date_gmt":"2016-10-12T06:34:37","guid":{"rendered":"http:\/\/justthesam.com\/?p=2015"},"modified":"2016-10-12T06:34:37","modified_gmt":"2016-10-12T06:34:37","slug":"fixing-java-net-inet6addressimpl-lookupallhostaddr-slowdown","status":"publish","type":"post","link":"https:\/\/justthesam.com\/?p=2015","title":{"rendered":"Fixing java.net.Inet6AddressImpl.lookupAllHostAddr slowdown"},"content":{"rendered":"<p>I was recently frustrated\u00a0by\u00a0very slow tests and timeouts in my Java code, that would often show a similar stack trace (if they actually timed out):<\/p>\n<pre>    io.vertx.core.VertxException: Thread blocked\n     at <span style=\"color: #800000;\"><strong>java.net.<\/strong><strong>Inet6AddressImpl.lookupAllHostAddr<\/strong><\/span>(Native Method)\n     at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928)\n     at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323)\n     at java.net.InetAddress.getLocalHost(InetAddress.java:1500)<\/pre>\n<p>I&#8217;ve highlighted the critical part. The Vertx bit is just\u00a0what implements the timeout in my case. If you&#8217;re using a Mac and you see this, you&#8217;re probably having the\u00a0same problem. You might also see <code>Inet4AddressImpl<\/code> in the stack trace instead of <code>Inet6AddressImpl<\/code>.<\/p>\n<p>After a lot of web trawling and some help from a colleague (thanks Tim) I got to the bottom of it. I&#8217;m writing it up here, for my own benefit when I run into this again in the future, and because a lot of the existing resources weren&#8217;t clear and direct enough to solve my problem easily.<\/p>\n<h2>The fix<\/h2>\n<p>The slowness is caused by a domain name lookup that&#8217;s taking a few seconds each time, because for some reason your computer\u00a0is\u00a0asking the network about its own address, and timing out. I don&#8217;t fully understand the mechanics frankly, but the fix was simple in my case.<\/p>\n<p>First, figure out what your computer thinks its hostname is, by running <code>hostname<\/code>\u00a0in the terminal. Then use the value returned from that to add lines like this to your \/etc\/hosts file:<\/p>\n<pre>    # This works around slow lookup that we sometimes see in\n    # java.net.Inet6AddressImpl.lookupAllHostAddr\n    127.0.0.1 Sams-MacBook-Pro.local\n    ::1 Sams-MacBook-Pro.local<\/pre>\n<p>This provides a direct answer for both IPv4 and IPv6, avoiding the slowness. This had the nice effect of bringing my Gradle build time down from 2 minutes to just 44 seconds, including all the tests.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was recently frustrated\u00a0by\u00a0very slow tests and timeouts in my Java code, that would often show a similar stack trace (if they actually timed out): io.vertx.core.VertxException: Thread blocked at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928) at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323) at java.net.InetAddress.getLocalHost(InetAddress.java:1500) I&#8217;ve highlighted the critical part. The Vertx bit is just\u00a0what implements the timeout in my case. If you&#8217;re [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,22],"tags":[],"class_list":["post-2015","post","type-post","status-publish","format-standard","hentry","category-mac","category-programming"],"_links":{"self":[{"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/posts\/2015","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/justthesam.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2015"}],"version-history":[{"count":0,"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/posts\/2015\/revisions"}],"wp:attachment":[{"href":"https:\/\/justthesam.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/justthesam.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/justthesam.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}