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.

AnnotatedConnectException: Connection refused: localhost/127.0.1.1:37655

See original GitHub issue

When there are more than one entries for localhost in /etc/hosts, connection is refused. This happens only when using DnsAddressResolverGroup. If DefaultAddressResolverGroup is used there is no problem with connection establishment.

# /etc/hosts
127.0.1.1	localhost
127.0.0.1	localhost

If 127.0.0.1 is first then there is no problem

# /etc/hosts
127.0.0.1	localhost
127.0.1.1	localhost

Original issue https://github.com/reactor/reactor-netty/issues/1405

Expected behavior

Connection to be established regardless of the AddressResolverGroup that is used.

Actual behavior

Exception is thrown with DnsAddressResolverGroup: AnnotatedConnectException: Connection refused: localhost/127.0.1.1:37655

Steps to reproduce

A reproducible example and steps are described in the issue https://github.com/reactor/reactor-netty/issues/1405

I added additional test to the existing test cases here https://github.com/hisener/playground/blob/master/spring-webclient-connection-failure-test/src/test/java/com/github/hisener/spring/webclient/WebClientTest.java

    @RepeatedTest(10)
    void nettyClient() throws Exception {
        NioEventLoopGroup group = new NioEventLoopGroup();

        try {
            Bootstrap bootstrap = new Bootstrap();

            bootstrap.group(group)
                     .channel(NioSocketChannel.class)
                     // There is no issue with DefaultAddressResolverGroup
                     //.resolver(DefaultAddressResolverGroup.INSTANCE)
                     .resolver(new DnsAddressResolverGroup(new DnsNameResolverBuilder(group.next()).channelType(NioDatagramChannel.class)))
                     .handler(new ChannelInitializer<SocketChannel>() {
                         @Override
                         protected void initChannel(SocketChannel socketChannel) {
                             socketChannel.pipeline()
                                          .addLast(new HttpClientCodec());
                         }
                     });

            Channel ch = bootstrap.connect(baseUrl.getHost(), baseUrl.getPort())
                                  .sync()
                                  .channel();

            ch.close();
        }
        finally {
            group.shutdownGracefully();
        }
    }

Netty version

4.1.55.Final-SNAPSHOT

JVM version (e.g. java -version)

openjdk version “11.0.9.1” 2020-11-04 OpenJDK Runtime Environment 18.9 (build 11.0.9.1+1) OpenJDK 64-Bit Server VM 18.9 (build 11.0.9.1+1, mixed mode)

OS version (e.g. uname -a)

Linux b732417723fd 5.4.39-linuxkit #1 SMP Fri May 8 23:03:06 UTC 2020 x86_64 GNU/Linux

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
violetaggcommented, May 11, 2021

@normanmaurer PTAL #11246 , I made some tricks to keep the backwards compatibility.

2reactions
violetaggcommented, Apr 3, 2021

@normanmaurer The current Netty implementations returns always only one address for localhost when resolveAll is invoked. If Netty is able to return all resolved addresses for localhost, Reactor Netty will be able to iterate over the result.

https://github.com/netty/netty/blob/6724786dcc9fa38ba516dab97a04e2bbc17e81d9/resolver/src/main/java/io/netty/resolver/DefaultHostsFileEntriesResolver.java#L46-L63

https://github.com/netty/netty/blob/6724786dcc9fa38ba516dab97a04e2bbc17e81d9/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java#L1023

https://github.com/netty/netty/blob/6724786dcc9fa38ba516dab97a04e2bbc17e81d9/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java#L684-L697

Also JDK’s HostsFileNameService returns all entries IPv4/IPv6 for localhost, while Netty’s HostsFileParser always returns the first for IPv4/IPv6. https://github.com/openjdk/jdk/blob/e8eda655bb27ab1b8d806a64622ea7ee9719a85d/src/java.base/share/classes/java/net/InetAddress.java#L1047-L1054

https://github.com/netty/netty/blob/6724786dcc9fa38ba516dab97a04e2bbc17e81d9/resolver/src/main/java/io/netty/resolver/HostsFileParser.java#L198-L209

Here is a simple test and its results:

@Test
void test() throws Exception {
    InetAddress[] addresses = InetAddress.getAllByName("localhost");
    System.out.println("JDK " + Arrays.asList(addresses));


    NioEventLoopGroup group = new NioEventLoopGroup();
    try {
        DefaultAddressResolverGroup.INSTANCE
                .getResolver(group.next())
                .resolveAll(InetSocketAddress.createUnresolved("localhost", 80))
                .addListener(future -> System.out.println("Netty DefaultAddressResolverGroup " + future.get()));

        DnsNameResolverBuilder builder = new DnsNameResolverBuilder(group.next())
                .channelType(NioDatagramChannel.class);
        DnsNameResolver resolver = builder.build();
        resolver.resolveAll("localhost")
                .addListener(future -> System.out.println("Netty DnsNameResolver " + future.get()));
    }
    finally {
        group.shutdownGracefully();
    }
}
JDK [localhost/127.0.0.1, localhost/127.0.1.1]
Netty DefaultAddressResolverGroup [localhost/127.0.0.1:80, localhost/127.0.1.1:80]
Netty DnsNameResolver [localhost/127.0.1.1]

Also I’m seeing that when I use JDK’s PlatformNameService, the result is sorted, while when I force the usage of HostsFileNameService, they are not.

So I need a method that returns all entries for localhost like in JDK’s implementation (preferably sorted). If it is not possible to modify the current interface (io.netty.resolver.HostsFileEntriesResolver), is it possible to introduce a new one? May be even completeOncePreferredResolved can be used.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

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