Skip to content

HTTP3: Dispose HTTP3 connection when HttpClientHandler is disposed #56943

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

Merged
merged 2 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public override async Task<HttpRequestData> HandleRequestAsync(HttpStatusCode st
}

// Wait for the client to close the connection, e.g. after we send a GOAWAY, or after the HttpClient is disposed.
public async Task WaitForClientDisconnectAsync()
public async Task WaitForClientDisconnectAsync(bool refuseNewRequests = true)
{
while (true)
{
Expand All @@ -204,6 +204,11 @@ public async Task WaitForClientDisconnectAsync()
try
{
stream = await AcceptRequestStreamAsync().ConfigureAwait(false);

if (!refuseNewRequests)
{
throw new Exception("Unexpected request stream received while waiting for client disconnect");
}
}
catch (QuicConnectionAbortedException abortException) when (abortException.ErrorCode == H3_NO_ERROR)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1942,6 +1942,12 @@ public void Dispose()
_associatedHttp2ConnectionCount -= (_availableHttp2Connections?.Count ?? 0);
_availableHttp2Connections?.Clear();

if (_http3Connection is not null)
{
toDispose.Add(_http3Connection);
_http3Connection = null;
}

if (_authorityExpireTimer != null)
{
_authorityExpireTimer.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,40 @@ public async Task ServerCertificateCustomValidationCallback_Succeeds()
Assert.Equal(1, invocationCount);
}

[Fact]
public async Task DisposeHttpClient_Http3ConnectionIsClosed()
{
using Http3LoopbackServer server = CreateHttp3LoopbackServer();

Task serverTask = Task.Run(async () =>
{
using Http3LoopbackConnection connection = (Http3LoopbackConnection)await server.EstablishGenericConnectionAsync();
HttpRequestData request = await connection.ReadRequestDataAsync();
await connection.SendResponseAsync();

await connection.WaitForClientDisconnectAsync(refuseNewRequests: false);
});

Task clientTask = Task.Run(async () =>
{
using HttpClient client = CreateHttpClient();
using HttpRequestMessage request = new()
{
Method = HttpMethod.Get,
RequestUri = server.Address,
Version = HttpVersion30,
VersionPolicy = HttpVersionPolicy.RequestVersionExact
};

using HttpResponseMessage response = await client.SendAsync(request);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);

// Return and let the HttpClient be disposed
});

await new[] { clientTask, serverTask }.WhenAllOrAnyFailed(20_000);
}

[OuterLoop]
[ConditionalTheory(nameof(IsMsQuicSupported))]
[MemberData(nameof(InteropUris))]
Expand Down