Skip to content

Instantly share code, notes, and snippets.

@tomfitzhenry
Last active March 6, 2018 09:56
Show Gist options
  • Save tomfitzhenry/083e715ea19fc55924755e0d6ce62b40 to your computer and use it in GitHub Desktop.
Save tomfitzhenry/083e715ea19fc55924755e0d6ce62b40 to your computer and use it in GitHub Desktop.
OpenJDK bug: Enabling JMX silently causes the disabling of the JVM's DNS cache to not work

Submitted to Oracle, but posted here too, because I'm not sure if this bug will make its way to the OpenJDK bug tracker.

Versions

$ java -version
openjdk version "1.8.0_121"
OpenJDK Runtime Environment (build 1.8.0_121-8u121-b13-0ubuntu1.16.04.2-b13)
OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)
$ javac -version
javac 1.8.0_121

Steps to reproduce

(You might have to run tshark and kill as root.)

$ javac MakeTwoDnsRequests.java
$ tshark -i any -f "src port 53" -Y "dns.qry.type eq 1" -n -T fields -e dns.qry.name -e dns.a > without-jmx.txt 2>/dev/null &
[1] 17460
$ java MakeTwoDnsRequests
$ kill 17460
$ cat without-jmx.txt
www.example.com 93.184.216.34
www.example.com 93.184.216.34
$ 
$ tshark -i any -f "src port 53" -Y "dns.qry.type eq 1" -n -T fields -e dns.qry.name -e dns.a > with-jmx.txt 2>/dev/null &
[1] 17495
$ java -Dcom.sun.management.jmxremote MakeTwoDnsRequests
$ kill 17495
$ cat with-jmx.txt

Expected

I expect two DNS requests are sent:

$ cat with-jmx.txt
www.example.com 93.184.216.34
www.example.com 93.184.216.34

Actual

But actually one DNS request is sent!

$ cat with-jmx.txt
www.example.com 93.184.216.34

Workaround

For Hotspot, specify sun.net.inetaddr.ttl=0 when starting the JVM:

$ java -Dsun.net.inetaddr.ttl=0 -Dcom.sun.management.jmxremote MakeTwoDnsRequests

My guess at the cause

networkaddress.cache.ttl is cached in the static initializer of InetAddressCachePolicy[1]. JMX presumably causes InetAddressCachePolicy's static initialiser to be executed, before main gets a chance to set networkaddress.cache.ttl.

  1. http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/8c93eb3fa1c0/src/share/classes/sun/net/InetAddressCachePolicy.java#l92
import java.security.Security;
import java.net.InetAddress;
public class MakeTwoDnsRequests {
public static void main(String[] args) throws Exception {
// Disable JVM DNS caching
Security.setProperty("networkaddress.cache.ttl", "0");
System.out.println(InetAddress.getByName("www.example.com").getHostAddress());
System.out.println(InetAddress.getByName("www.example.com").getHostAddress());
}
}
@tomfitzhenry
Copy link
Author

Oracle bug with internal ID 9047808.

@tomfitzhenry
Copy link
Author

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