From f1145fb6d7f418b3f1987f94e9f457f8f4c2b18b Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Thu, 2 Apr 2020 09:02:09 +1300 Subject: [PATCH] =?UTF-8?q?Fix=20flaky=20ClientStreaming=5FResponseComplet?= =?UTF-8?q?esWithoutResponseBody=E2=80=A6=20(#20338)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TestHost/src/HttpContextBuilder.cs | 35 +++++++++---------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/src/Hosting/TestHost/src/HttpContextBuilder.cs b/src/Hosting/TestHost/src/HttpContextBuilder.cs index f425a55b2d..736b0458a6 100644 --- a/src/Hosting/TestHost/src/HttpContextBuilder.cs +++ b/src/Hosting/TestHost/src/HttpContextBuilder.cs @@ -115,10 +115,25 @@ namespace Microsoft.AspNetCore.TestHost // This could throw an error if there was a pending server read. Needs to // happen before completing the response so the response returns the error. var requestBodyInProgress = RequestBodyReadInProgress(); + if (requestBodyInProgress) + { + // If request is still in progress then abort it. + CancelRequestBody(); + } // Matches Kestrel server: response is completed before request is drained await CompleteResponseAsync(); - await CompleteRequestAsync(requestBodyInProgress); + + if (!requestBodyInProgress) + { + // Writer was already completed in send request callback. + await _requestPipe.Reader.CompleteAsync(); + + // Don't wait for request to drain. It could block indefinitely. In a real server + // we would wait for a timeout and then kill the socket. + // Potential future improvement: add logging that the request timed out + } + _application.DisposeContext(_testContext, exception: null); } catch (Exception ex) @@ -165,24 +180,6 @@ namespace Microsoft.AspNetCore.TestHost CancelRequestBody(); } - private async Task CompleteRequestAsync(bool requestBodyInProgress) - { - if (requestBodyInProgress) - { - // If request is still in progress then abort it. - CancelRequestBody(); - } - else - { - // Writer was already completed in send request callback. - await _requestPipe.Reader.CompleteAsync(); - } - - // Don't wait for request to drain. It could block indefinitely. In a real server - // we would wait for a timeout and then kill the socket. - // Potential future improvement: add logging that the request timed out - } - private bool RequestBodyReadInProgress() { try