Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Dns.Resolve issue by using ContinueWith and AggregateException.Flatten #5260

Merged
merged 15 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/core/Akka.Tests/IO/TcpIntegrationSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
using Xunit.Abstractions;
using FluentAssertions;
using System.Runtime.InteropServices;
using System.Threading;
using Akka.Event;

namespace Akka.Tests.IO
{
Expand Down Expand Up @@ -470,6 +472,19 @@ public void The_TCP_transport_implementation_dont_report_Connected_when_endpoint
replies.Count.ShouldBe(0);
}

[Fact]
public void Should_report_Error_only_once_when_connecting_to_unreachable_DnsEndpoint()
{
var probe = CreateTestProbe();
// a "random" endpoint hopefully unavailable since it's in the test-net IP range
var endpoint = new DnsEndPoint("fake", 1000);
Sys.Tcp().Tell(new Tcp.Connect(endpoint), probe.Ref);

// expecting CommandFailed or no reply (within timeout)
var replies = probe.ReceiveWhile(TimeSpan.FromSeconds(5), x => x as Tcp.CommandFailed);
replies.Count.ShouldBe(1);
}

[Fact]
public void The_TCP_transport_implementation_handle_tcp_connection_actor_death_properly()
{
Expand Down
47 changes: 30 additions & 17 deletions src/core/Akka/IO/InetAddressDnsResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Configuration;

Expand Down Expand Up @@ -43,28 +45,39 @@ public InetAddressDnsResolver(SimpleDnsCache cache, Config config)
/// <returns>TBD</returns>
protected override bool Receive(object message)
{
var resolve = message as Dns.Resolve;
if (resolve != null)
if (message is Dns.Resolve resolve)
{
var answer = _cache.Cached(resolve.Name);
if (answer == null)
if (answer != null)
{
try
{
//TODO: IP6
answer = Dns.Resolved.Create(resolve.Name, System.Net.Dns.GetHostEntryAsync(resolve.Name).Result.AddressList.Where(x =>
x.AddressFamily == AddressFamily.InterNetwork
|| _useIpv6 && x.AddressFamily == AddressFamily.InterNetworkV6));
_cache.Put(answer, _positiveTtl);
}
catch (SocketException ex)
Sender.Tell(answer);
return true;
}

System.Net.Dns.GetHostEntryAsync(resolve.Name).ContinueWith(t =>
{
Dns.Resolved newAnswer;
if (t.IsFaulted)
{
if (ex.SocketErrorCode != SocketError.HostNotFound) throw;
answer = new Dns.Resolved(resolve.Name, Enumerable.Empty<IPAddress>(), Enumerable.Empty<IPAddress>());
_cache.Put(answer, _negativeTtl);
foreach (var exception in t.Exception.Flatten().InnerExceptions)
{
if (exception is SocketException se && se.SocketErrorCode == SocketError.HostNotFound)
{
newAnswer = new Dns.Resolved(resolve.Name, Enumerable.Empty<IPAddress>(), Enumerable.Empty<IPAddress>());
_cache.Put(newAnswer, _negativeTtl);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't cache failures, since they can be intermittent

return newAnswer;
}
}
ExceptionDispatchInfo.Capture(t.Exception).Throw();
}
}
Sender.Tell(answer);

newAnswer = Dns.Resolved.Create(resolve.Name, t.Result.AddressList.Where(x =>
x.AddressFamily == AddressFamily.InterNetwork
|| _useIpv6 && x.AddressFamily == AddressFamily.InterNetworkV6));
_cache.Put(newAnswer, _positiveTtl);
return newAnswer;

}, TaskContinuationOptions.ExecuteSynchronously).PipeTo(Sender);
Aaronontheweb marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
return false;
Expand Down