Closed
Description
Description
The following program is supposed to resolve multiple hostNameOrAddress
es by Dns.GetHostAddressesAsync in parallel. However, the program gets stuck when run on Windows 10 (or 11) but it works as expected on the latest Ubuntu.
Reproduction Steps
App.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
Program.cs
using System.Net;
namespace DnsCancelling;
public class Program
{
public static async Task Main(string[] args)
{
string invalidAddress = "59FB::1005:CC57::";
if (args.Length == 0)
{
bool result = await ResolveManyAsync(invalidAddress).ConfigureAwait(false);
Console.WriteLine("Result 1 is {0}.", result);
Console.WriteLine();
}
string[] addresses = new[]
{
invalidAddress,
"localhost",
};
bool result2 = await ResolveManyAsync(addresses).ConfigureAwait(false);
Console.WriteLine("Result 2 is {0}.", result2);
}
private static async Task<bool> ResolveManyAsync(params string[] addresses)
{
using CancellationTokenSource cts = new();
Console.WriteLine("Going to resolve {0} addresses.", addresses.Length);
List<Task<bool>> resolveTasks = new(capacity: addresses.Length);
foreach (string address in addresses)
{
Task<bool> resolveTask = ResolveOneAsync(address, cts);
resolveTasks.Add(resolveTask);
}
bool[] results = await Task.WhenAll(resolveTasks).ConfigureAwait(false);
return results.All(r => r);
}
private static async Task<bool> ResolveOneAsync(string address, CancellationTokenSource cancellationTokenSource)
{
bool result = true;
IPAddress[] ipAddresses;
try
{
Console.WriteLine("Resolving address '{0}'.", address);
ipAddresses = await Dns.GetHostAddressesAsync(address, cancellationTokenSource.Token).ConfigureAwait(false);
if (ipAddresses.Length > 0) Console.WriteLine("'{0}' resolved to {1}.", address, ipAddresses[0]);
else Console.WriteLine("'{0}' can not be resolved.", address);
}
catch (Exception e)
{
Console.WriteLine("Canceling all due to an exception when resolving '{0}': {1}", address, e.Message);
cancellationTokenSource.Cancel();
Console.WriteLine("Cancellation complete.");
result = false;
}
return result;
}
}
Run the sample using dotnet build && dotnet run
using CLI.
Expected behavior
The program should terminate. I verified that the program terminates on the latest Ubuntu version.
Actual behavior
The program gets stuck and never finishes when it is run on Windows.
Regression?
No response
Known Workarounds
No response
Configuration
- .NET 6
- Windows 10, 11.
Other information
No response