From 6d072223dcfc80ee314007b4bce929465130b8ef Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Fri, 6 Nov 2020 16:51:41 -0500 Subject: [PATCH] Fix Send_TimeoutResponseContent_Throws (#44356) If the client times out too quickly, the server may never have a connection to accept and will hang forever. --- .../tests/FunctionalTests/HttpClientTest.cs | 55 ++++++++----------- 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientTest.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientTest.cs index 94a15bea34cf7..a267ced0b67cc 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientTest.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientTest.cs @@ -1076,42 +1076,33 @@ await server.AcceptConnectionAsync(async connection => [Fact] [OuterLoop] - public async Task Send_TimeoutResponseContent_Throws() + public void Send_TimeoutResponseContent_Throws() { - string content = "Test content"; + const string Content = "Test content"; - await LoopbackServer.CreateClientAndServerAsync( - async uri => - { - var sendTask = Task.Run(() => { - using HttpClient httpClient = CreateHttpClient(); - httpClient.Timeout = TimeSpan.FromSeconds(0.5); - HttpResponseMessage response = httpClient.Send(new HttpRequestMessage(HttpMethod.Get, uri)); - }); + using var server = new LoopbackServer(); - TaskCanceledException ex = await Assert.ThrowsAsync(() => sendTask); - Assert.IsType(ex.InnerException); - }, - async server => + // Ignore all failures from the server. This includes being disposed of before ever accepting a connection, + // which is possible if the client times out so quickly that it hasn't initiated a connection yet. + _ = server.AcceptConnectionAsync(async connection => + { + await connection.ReadRequestDataAsync(); + await connection.SendResponseAsync(headers: new[] { new HttpHeaderData("Content-Length", (Content.Length * 100).ToString()) }); + for (int i = 0; i < 100; ++i) { - await server.AcceptConnectionAsync(async connection => - { - try - { - await connection.ReadRequestDataAsync(); - await connection.SendResponseAsync(headers: new List() { - new HttpHeaderData("Content-Length", (content.Length * 100).ToString()) - }); - for (int i = 0; i < 100; ++i) - { - await connection.Writer.WriteLineAsync(content); - await connection.Writer.FlushAsync(); - await Task.Delay(TimeSpan.FromSeconds(0.1)); - } - } - catch { } - }); - }); + await connection.Writer.WriteLineAsync(Content); + await connection.Writer.FlushAsync(); + await Task.Delay(TimeSpan.FromSeconds(0.1)); + } + }); + + TaskCanceledException ex = Assert.Throws(() => + { + using HttpClient httpClient = CreateHttpClient(); + httpClient.Timeout = TimeSpan.FromSeconds(0.5); + HttpResponseMessage response = httpClient.Send(new HttpRequestMessage(HttpMethod.Get, server.Address)); + }); + Assert.IsType(ex.InnerException); } public static IEnumerable VersionSelectionMemberData()