JDK-8170910: Avoid 5 second localhost lookup behavior on OSX

Nora Howard nhoward at twitter.com
Tue Apr 2 22:35:26 UTC 2019


On OSX 10.12 or later, when no mDNS services are registered, getaddrinfo
fails on looking up localhost addresses[1]. The jdk’s current
implementation falls back to getifaddrs after getaddrinfo fails[2]. When
getaddrinfo fails in this way, it takes 5 seconds to fail [3]. This means
that calls to InetAddress.getLocalHost() may take up to 5 seconds to run.

I’d like to change the jdk’s behavior in this instance so that instead of
falling back to calling getifaddrs after getaddrinfo fails, it instead
calls getifaddrs first if we’re on OSX and the hostname is localhost’s.

Doing this will eliminate the 5 second delay looking up localhost on
machines with this network setup. When looking up non-localhost addresses,
it’ll add a call to os:gethostname and a comparison.

I’d like to get some feedback about whether this approach sounds
reasonable, and some guidance about next steps. I’ve read the “how to
contribute” page[4], and the “code review” guide[5].

I’ve put together a patch that I’ve tested locally by turning off mDNS
services on my local OSX machine and exercising the behavior. I’d like to
submit patches that target dev trunk as well as the jdk 8 and 11 branches.

References

1.

> On a Macintosh running 10.12 or later, if there are no apps that have a
> mDNS services registered (for instance, iTunes) and there are no services
> selected in the Sharing System Preferences, then the getaddrinfo will fail.
>
(https://bugs.openjdk.java.net/browse/JDK-8170910)

2.
http://hg.openjdk.java.net/jdk/jdk/file/cd3b7ad53265/src/java.base/unix/native/libnet/Inet6AddressImpl.c#l252
http://hg.openjdk.java.net/jdk/jdk/file/cd3b7ad53265/src/java.base/unix/native/libnet/Inet4AddressImpl.c#l132
(The lookupIfLocalhost function checks the passed hostname against
os::get_host_name, and if they match, looks up via getifaddrs.)

3. This comment describes how to reproduce the issue. I’ve reproduced it
locally on jdk 8, 11 and 13 ea.
https://bugs.openjdk.java.net/browse/JDK-8170910?focusedCommentId=14038262&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-14038262

4. https://openjdk.java.net/contribute/

5. https://openjdk.java.net/guide/codeReview.html


Thanks,
--
Nora


More information about the jdk-dev mailing list