Repository: dotnet/runtime
File: src/libraries/System.Net.NameResolution/src/System/Net/Dns.cs (C#)

Code before:
            }
            else
            {
                if (NameResolutionPal.SupportsGetAddrInfoAsync)
                {
#pragma warning disable CS0162 // Unreachable code detected -- SupportsGetAddrInfoAsync is a constant on *nix.

                    // If the OS supports it and 'hostName' is not an IP Address, resolve the name asynchronously
                    // instead of calling the synchronous version in the ThreadPool.

                    ValidateHostName(hostName);

                    Task t;
                    if (NameResolutionTelemetry.Log.IsEnabled())
                    {
                        t = justAddresses
                            ? (Task)GetAddrInfoWithTelemetryAsync<IPAddress[]>(hostName, justAddresses, family, cancellationToken)
                            : (Task)GetAddrInfoWithTelemetryAsync<IPHostEntry>(hostName, justAddresses, family, cancellationToken);
                    }
                    else
                    {
                        t = NameResolutionPal.GetAddrInfoAsync(hostName, justAddresses, family, cancellationToken);
                    }


                    if (!t.IsFaulted || !(t.Exception?.InnerException is InvalidOperationException))
                    {
                        return t;
                    }
                    // If we failed with InvalidOperationException fall-through and use synchronous lookup on threadpool.
                }

                asyncState = family == AddressFamily.Unspecified ? (object)hostName : new KeyValuePair<string, AddressFamily>(hostName, family);
            }

            if (justAddresses)
            {
                return RunAsync(s => s switch
                {
                    string h => GetHostAddressesCore(h, AddressFamily.Unspecified),
                    KeyValuePair<string, AddressFamily> t => GetHostAddressesCore(t.Key, t.Value),
                    IPAddress a => GetHostAddressesCore(a, AddressFamily.Unspecified),
                    KeyValuePair<IPAddress, AddressFamily> t => GetHostAddressesCore(t.Key, t.Value),
                    _ => null
                }, asyncState);
            }
            else
            {
                return RunAsync(s => s switch
                {
                    string h => GetHostEntryCore(h, AddressFamily.Unspecified),

Review comment (bug):
I'm a learner at the task based async model, but it looks like `IsFaulted` does not wait for completion. Is the task guaranteed to have completed by the time it is returned by GetAddrInfoAsync? If not, is there a race here? 

Another option maybe is to wrap this task in another, which knows to go the other route if the inner task fails.

Code after:
            }
            else
            {
                if (NameResolutionPal.SupportsGetAddrInfoAsync)
                {
#pragma warning disable CS0162 // Unreachable code detected -- SupportsGetAddrInfoAsync is a constant on *nix.

                    // If the OS supports it and 'hostName' is not an IP Address, resolve the name asynchronously
                    // instead of calling the synchronous version in the ThreadPool.
                    // If it fails, we will fall back to ThreadPool as well.

                    ValidateHostName(hostName);

                    Task? t;
                    if (NameResolutionTelemetry.Log.IsEnabled())
                    {
                        t = justAddresses
                            ? GetAddrInfoWithTelemetryAsync<IPAddress[]>(hostName, justAddresses, family, cancellationToken)
                            : GetAddrInfoWithTelemetryAsync<IPHostEntry>(hostName, justAddresses, family, cancellationToken);

                    }
                    else
                    {
                        t = NameResolutionPal.GetAddrInfoAsync(hostName, justAddresses, family, cancellationToken);
                    }

                    // If async resolution started, return task to user. otherwise fall back to sync API on threadpool.
                    if (t != null)
                    {
                        return t;
                    }
                }

                asyncState = family == AddressFamily.Unspecified ? (object)hostName : new KeyValuePair<string, AddressFamily>(hostName, family);
            }

            if (justAddresses)
            {
                return RunAsync(s => s switch
                {
                    string h => GetHostAddressesCore(h, AddressFamily.Unspecified),
                    KeyValuePair<string, AddressFamily> t => GetHostAddressesCore(t.Key, t.Value),
                    IPAddress a => GetHostAddressesCore(a, AddressFamily.Unspecified),
                    KeyValuePair<IPAddress, AddressFamily> t => GetHostAddressesCore(t.Key, t.Value),
                    _ => null
                }, asyncState);
            }
            else
            {
                return RunAsync(s => s switch
                {