question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to connect to host using DNS resolver on Java 9+

See original GitHub issue

Expected behavior

Using Java 16/17 I can connect with a host by host name successfully, just as with Java 8

Actual behavior

When running the same code with Java 8, I can successfully connect to a remote host by name, but with Java 9, 16 and 17 I cannot.

Stacktrace: java.lang.ExceptionInInitializerError at xxx Caused by: java.net.UnknownHostException: failed to resolve ‘somehost’ after 10 queries at io.netty.resolver.dns.DnsResolveContext.finishResolve(DnsResolveContext.java:1046) at io.netty.resolver.dns.DnsResolveContext.tryToFinishResolve(DnsResolveContext.java:999) at io.netty.resolver.dns.DnsResolveContext.query(DnsResolveContext.java:417) at io.netty.resolver.dns.DnsResolveContext.onResponse(DnsResolveContext.java:628) at io.netty.resolver.dns.DnsResolveContext.access$400(DnsResolveContext.java:65) at io.netty.resolver.dns.DnsResolveContext$2.operationComplete(DnsResolveContext.java:461) at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:578) at io.netty.util.concurrent.DefaultPromise.notifyListeners0(DefaultPromise.java:571) at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:550) at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:491) at io.netty.util.concurrent.DefaultPromise.setValue0(DefaultPromise.java:616) at io.netty.util.concurrent.DefaultPromise.setSuccess0(DefaultPromise.java:605) at io.netty.util.concurrent.DefaultPromise.trySuccess(DefaultPromise.java:104) at io.netty.resolver.dns.DnsQueryContext.trySuccess(DnsQueryContext.java:200) at io.netty.resolver.dns.DnsQueryContext.finish(DnsQueryContext.java:192) at io.netty.resolver.dns.DnsNameResolver$DnsResponseHandler.channelRead(DnsNameResolver.java:1301) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357) at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357) at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919) at io.netty.channel.nio.AbstractNioMessageChannel$NioMessageUnsafe.read(AbstractNioMessageChannel.java:97) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:719) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493) at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986) at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) at java.base/java.lang.Thread.run(Thread.java:833)

Steps to reproduce

I am using Redisson, which is using Netty:

    Config config = new Config();
    config.useSingleServer()
            .setAddress("redis://somehost:5379");
    CLIENT = Redisson.create(config);

Minimal yet complete reproducer code (or URL to code)

Netty version

tried 4.1.68-FINAL and 4.1.70-FINAL (via Redisson)

JVM version (e.g. java -version)

9.0.4, 17.0.1

OS version (e.g. uname -a)

Window 10 21H2

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
flupeccommented, Aug 17, 2022

Faced same issue with Spring WebClient that’s based on Netty. DNS suffixes cannot be resolved on Windows and Java 9+. I think this hack is responsible for such behaviour.

Why is reflection used here? You can acquire instance of ResolverConfiguration via static method, no?

0reactions
dlehammercommented, Sep 19, 2022

I’ve experienced the symptom similarly to @flupec in a Spring Boot context, albeit in a Linux environment (Fedora 36 & Java 17). I.e. Netty unable to resolve the local machine alias ~ $HOSTNAME.

I found a workaround; explicitly adding the machine alias to /etc/hosts seemed to appease Netty (no other services has needed this, and ping etc. resolved the machine alias fine before I made any changes!)

127.0.0.1 my-machine-alias my-machine-alias.localdomain

I too am unable to understand why Netty tries to handle a responsibility owned by the OS, i.e. I somewhat understand the desire to cache resolved DNS-data, but caching it inside each service running on a OS is just asking for support tickets…

I’m not that familiar with the Windows DNS cache support, but on Linux most distributions provide a OS level cache for this specific purpose, ex. https://manpages.ubuntu.com/manpages/trusty/man8/dnscache.8.html 🤓

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to connect to host using DNS resolver on Java 9+ #4004
When running the same code with Java 8, I can successfully connect to a remote host by name, but with Java 9, 16...
Read more >
How to configure the DNS server java should use in jdk 9, 10 ...
For testing you should use an alternative hosts file that could be defined by the -Djdk.net.hosts.file=path_to_alternative_hosts_file JVM option ...
Read more >
Troubleshoot the UnknownHostException error in a Java ...
UnknownHostException is a common error message in Java applications. This error typically indicates that there was a DNS resolution failure.
Read more >
Java doesn't pick up system's DNS settings change until ...
Hello, I have a service running on a few Linux computers. Those computers have a NIC, which is configured with a fixed IP...
Read more >
Troubleshooting | Public DNS - Google Developers
If your browser can't find the dns.google server, or it doesn't ... Use Flush Cache (documentation) to flush the registered domain and the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found