Skip to content

Commit f1145fb

Browse files
authored
Fix flaky ClientStreaming_ResponseCompletesWithoutResponseBody… (#20338)
1 parent 7d450bb commit f1145fb

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

src/Hosting/TestHost/src/HttpContextBuilder.cs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,25 @@ async Task RunRequestAsync()
115115
// This could throw an error if there was a pending server read. Needs to
116116
// happen before completing the response so the response returns the error.
117117
var requestBodyInProgress = RequestBodyReadInProgress();
118+
if (requestBodyInProgress)
119+
{
120+
// If request is still in progress then abort it.
121+
CancelRequestBody();
122+
}
118123

119124
// Matches Kestrel server: response is completed before request is drained
120125
await CompleteResponseAsync();
121-
await CompleteRequestAsync(requestBodyInProgress);
126+
127+
if (!requestBodyInProgress)
128+
{
129+
// Writer was already completed in send request callback.
130+
await _requestPipe.Reader.CompleteAsync();
131+
132+
// Don't wait for request to drain. It could block indefinitely. In a real server
133+
// we would wait for a timeout and then kill the socket.
134+
// Potential future improvement: add logging that the request timed out
135+
}
136+
122137
_application.DisposeContext(_testContext, exception: null);
123138
}
124139
catch (Exception ex)
@@ -165,24 +180,6 @@ internal void ClientInitiatedAbort()
165180
CancelRequestBody();
166181
}
167182

168-
private async Task CompleteRequestAsync(bool requestBodyInProgress)
169-
{
170-
if (requestBodyInProgress)
171-
{
172-
// If request is still in progress then abort it.
173-
CancelRequestBody();
174-
}
175-
else
176-
{
177-
// Writer was already completed in send request callback.
178-
await _requestPipe.Reader.CompleteAsync();
179-
}
180-
181-
// Don't wait for request to drain. It could block indefinitely. In a real server
182-
// we would wait for a timeout and then kill the socket.
183-
// Potential future improvement: add logging that the request timed out
184-
}
185-
186183
private bool RequestBodyReadInProgress()
187184
{
188185
try

0 commit comments

Comments
 (0)