Skip to content

Instantly share code, notes, and snippets.

@jedp
Last active February 17, 2022 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jedp/249a6eaa9a2dc56a32d7efaf671ace8e to your computer and use it in GitHub Desktop.
Save jedp/249a6eaa9a2dc56a32d7efaf671ace8e to your computer and use it in GitHub Desktop.
Fix for InetAddress.getLocalHost on apple M1: Prevent hanging on inane DNS lookup

Fixing Ludicrous Java Localhost DNS Lookup on Mac

I recently started using the excellent adm wrapper for the android debug bridge.

I quickly noticed that my app was hanging on startup on an aarch_64 (Apple M1) machine.

Fortunately, vert.x, a dependency of adm, gave the following warning:

[vertx-blocked-thread-checker] WARN io.vertx.core.impl.BlockedThreadChecker - Thread Thread[vert.x-eventloop-thread-0,5,main] has been blocked for 4985 ms, time limit is 2000 ms
io.vertx.core.VertxException: Thread blocked

Doing a local build of adm enabled me to configure vert.x to set its warningExceptionTime to 1 second.

// For example:
private val vertx by lazy {
  Vertx.vertx(VertxOptions().apply {
    warningExceptionTime = TimeUnit.SECONDS.toNanos(1L)
  })
}

After this, it additionally shared the stack trace:

        at java.base@11.0.13/java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method)
        at java.base@11.0.13/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:929)
        at java.base@11.0.13/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1519)
        at java.base@11.0.13/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:848)
        at java.base@11.0.13/java.net.InetAddress.getAllByName0(InetAddress.java:1509)
        at java.base@11.0.13/java.net.InetAddress.getLocalHost(InetAddress.java:1641)

This shows that InetAddress.getLocalHost() is unable to figure out the address of my-macbookpro.local and is sending out DNS queries to find it. Oof.

This quick test will show if you are affected: inetTester.

I am aware of three two ways to fix the problem.

1. Add your hostname to your /etc/hosts file.

Execute hostname in a shell and put the result in your /etc/hosts like so:

# Prevent poor java.net.InetAddress.getLocalHost from hanging.
127.0.0.1 your-macbookpro.local
::1 your-macbookpro.local

2. Use /etc/resolver/ and run dnsmasq

No. I don't think this works. At least I haven't gotten it to work.

Preferable to 1. for long-term fixes.

Create the directory /etc/resolver/ if it does not exist.

Create a new file /etc/resolver/local and add your hostname info.

Start dnsmasq.

3. Configure jdk.net.hosts.file

Gloriously, you can do this. Simply run with

-Djdk.net.hosts.file=/path/to/hosts/file

You could construct that new file by copying /etc/hosts and appending your hostname info. This offers some flexibility by removing the need to update your users' /etc files directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment