-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Improve reliability of HttpRequestError.NameResolutionError #89948
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
Changes from 5 commits
6008034
53a810d
639174b
6eebbda
11488d8
7111a07
6859a75
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -143,13 +143,17 @@ internal static Exception CreateWrappedException(Exception exception, string hos | |
|
|
||
| static HttpRequestError DeduceError(Exception exception) | ||
| { | ||
| // TODO: Deduce quic errors from QuicException.TransportErrorCode once https://github.com/dotnet/runtime/issues/87262 is implemented. | ||
| if (exception is AuthenticationException) | ||
| { | ||
| return HttpRequestError.SecureConnectionError; | ||
| } | ||
|
|
||
| if (exception is SocketException socketException && socketException.SocketErrorCode == SocketError.HostNotFound) | ||
| // Resolving a non-existent hostname often leads to EAI_AGAIN/TryAgain on Linux, indicating a non-authoritative failure, eg. timeout. | ||
| // Getting EAGAIN/TryAgain from a TCP connect() is not possible on Windows or Mac according to the docs and indicates lack of kernel resources on Linux, | ||
| // which should be a very rare error in practice. As a result, mapping SocketError.TryAgain to HttpRequestError.NameResolutionError | ||
| // leads to a more reliable distinction between NameResolutionError and ConnectionError. | ||
| if (exception is SocketException socketException && | ||
| socketException.SocketErrorCode is SocketError.HostNotFound or SocketError.TryAgain) | ||
|
Comment on lines
+155
to
+156
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you wanted to, since you're already using pattern matching, you could make this even more concise: exception is SocketException { SocketErrorCode: SocketError.HostNotFound or SocketError.TryAgain } |
||
| { | ||
| return HttpRequestError.NameResolutionError; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Net.Sockets; | ||
| using System.Security.Cryptography.X509Certificates; | ||
| using System.Threading; | ||
|
|
@@ -18,10 +20,12 @@ public sealed class QuicConnectionTests : QuicTestBase | |
|
|
||
| public QuicConnectionTests(ITestOutputHelper output) : base(output) { } | ||
|
|
||
| [Fact] | ||
| public async Task TestConnect() | ||
| [Theory] | ||
| [InlineData(false)] | ||
| [InlineData(true)] | ||
| public async Task TestConnect(bool ipv6) | ||
| { | ||
| await using QuicListener listener = await CreateQuicListener(); | ||
| await using QuicListener listener = await CreateQuicListener(ipv6 ? IPAddress.IPv6Loopback : IPAddress.Loopback); | ||
|
|
||
| var options = CreateQuicClientOptions(listener.LocalEndPoint); | ||
| ValueTask<QuicConnection> connectTask = CreateQuicConnection(options); | ||
|
|
@@ -31,15 +35,27 @@ public async Task TestConnect() | |
| await using QuicConnection serverConnection = acceptTask.Result; | ||
| await using QuicConnection clientConnection = connectTask.Result; | ||
|
|
||
| Assert.Equal(listener.LocalEndPoint, serverConnection.LocalEndPoint); | ||
| Assert.Equal(listener.LocalEndPoint, clientConnection.RemoteEndPoint); | ||
| Assert.Equal(clientConnection.LocalEndPoint, serverConnection.RemoteEndPoint); | ||
| IgnoreScopeIdIPEndpointComparer endPointComparer = new(); | ||
| Assert.Equal(listener.LocalEndPoint, serverConnection.LocalEndPoint, endPointComparer); | ||
| Assert.Equal(listener.LocalEndPoint, clientConnection.RemoteEndPoint, endPointComparer); | ||
| Assert.Equal(clientConnection.LocalEndPoint, serverConnection.RemoteEndPoint, endPointComparer); | ||
|
Comment on lines
+38
to
+41
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To improve confidence in my changes, I extended this test to test IPv6.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. scopeID should be only relevant for LLA. I'm wondering if we are deviating from Socket/NetworkStream here because the underlying MSQuic implementation.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's possible. If we think it's a problem we can track it in a separate issue.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like this was existing condition. We miss it because QuicConnectionTests.TestConnect only tests IPv4. I sync back and I tried it with IPv6 and it also fails: I will fix it tomorrow. |
||
| Assert.Equal(ApplicationProtocol.ToString(), clientConnection.NegotiatedApplicationProtocol.ToString()); | ||
| Assert.Equal(ApplicationProtocol.ToString(), serverConnection.NegotiatedApplicationProtocol.ToString()); | ||
| Assert.Equal(options.ClientAuthenticationOptions.TargetHost, clientConnection.TargetHostName); | ||
| Assert.Equal(options.ClientAuthenticationOptions.TargetHost, serverConnection.TargetHostName); | ||
| } | ||
|
|
||
| private class IgnoreScopeIdIPEndpointComparer : IEqualityComparer<IPEndPoint> | ||
| { | ||
| public bool Equals(IPEndPoint x, IPEndPoint y) | ||
| { | ||
| byte[] xBytes = x.Address.GetAddressBytes(); | ||
| byte[] yBytes = y.Address.GetAddressBytes(); | ||
| return xBytes.AsSpan().SequenceEqual(yBytes) && x.Port == y.Port; | ||
| } | ||
| public int GetHashCode([DisallowNull] IPEndPoint obj) => obj.Port; | ||
| } | ||
|
|
||
| private static async Task<QuicStream> OpenAndUseStreamAsync(QuicConnection c) | ||
| { | ||
| QuicStream s = await c.OpenOutboundStreamAsync(QuicStreamType.Bidirectional); | ||
|
|
@@ -369,17 +385,18 @@ await Task.WhenAll( | |
| }).WaitAsync(TimeSpan.FromSeconds(5))); | ||
| } | ||
|
|
||
| [Fact] | ||
| [OuterLoop("Uses external servers")] | ||
| public async Task ConnectAsync_InvalidName_ThrowsSocketException() | ||
| [Theory] | ||
| [InlineData(false)] | ||
| [InlineData(true)] | ||
| public async Task ConnectAsync_InvalidName_ThrowsSocketException(bool sameTargetHost) | ||
| { | ||
| string name = $"{Guid.NewGuid().ToString("N")}.microsoft.com."; | ||
| var options = new QuicClientConnectionOptions() | ||
| { | ||
| DefaultStreamErrorCode = DefaultStreamErrorCodeClient, | ||
| DefaultCloseErrorCode = DefaultCloseErrorCodeClient, | ||
| RemoteEndPoint = new DnsEndPoint(name, 10000), | ||
| ClientAuthenticationOptions = GetSslClientAuthenticationOptions() | ||
| ClientAuthenticationOptions = GetSslClientAuthenticationOptions(sameTargetHost ? name : "localhost") | ||
| }; | ||
|
|
||
| SocketException ex = await Assert.ThrowsAsync<SocketException>(() => QuicConnection.ConnectAsync(options).AsTask()); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.