-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Dns.GetHostAddressesAsync
gets stuck on Windows
#63552
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @dotnet/ncl Issue DetailsDescriptionThe following program is supposed to resolve multiple Reproduction Steps
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
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;
}
} Expected behaviorThe program gets stuck and never finishes when it is run on Windows. Actual behaviorThe program should terminate. I verified that the program terminates on the latest Ubuntu version. Regression?No response Known WorkaroundsNo response ConfigurationTested on:
Other informationNo response
|
I can't repro, program finishes nearly instantly for me on Win11. |
@tomrus88 Ah, I forgot to add that I run the program using |
Also it's good idea to run the sample 10 times in a row. |
It certainly does look like that. That said, I'm surprised that the GetAddrInfoExCancel call is blocking waiting on the callback to complete. Regardless, it seems like we need to avoid taking our managed lock during the callback. |
Would it remove the need for the lock, if we used runtime/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Windows.cs Lines 407 to 429 in 9529c35
runtime/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Windows.cs Lines 433 to 442 in 9529c35
/cc @scalablecory as author of #33420. |
Triage: Deadlock in new API in .NET 6.0. We should fix it and backport it to .NET 6.0 servicing if we have customers hitting it. |
@karelz Our project is not yet released but we would love to use the code snippet in the original post once we are production ready. |
Reopening to track 6.0.* backport. We need a validation with the original console app. |
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
Program.cs
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
Other information
No response
The text was updated successfully, but these errors were encountered: