Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit 399ac26

Browse files
Cancel previous request on close (#2923)
1 parent 753c3da commit 399ac26

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

src/Microsoft.AspNetCore.Http.Connections/Internal/HttpConnectionDispatcher.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,18 @@ private async Task ExecuteAsync(HttpContext context, ConnectionDelegate connecti
215215
// Cancel the previous request
216216
connection.Cancellation?.Cancel();
217217

218-
// Wait for the previous request to drain
219-
await connection.PreviousPollTask;
218+
try
219+
{
220+
// Wait for the previous request to drain
221+
await connection.PreviousPollTask;
222+
}
223+
catch (OperationCanceledException)
224+
{
225+
// Previous poll canceled due to connection closing, close this poll too
226+
context.Response.ContentType = "text/plain";
227+
context.Response.StatusCode = StatusCodes.Status204NoContent;
228+
return;
229+
}
220230

221231
connection.PreviousPollTask = currentRequestTcs.Task;
222232
}
@@ -286,6 +296,9 @@ private async Task ExecuteAsync(HttpContext context, ConnectionDelegate connecti
286296
// If the status code is a 204 it means the connection is done
287297
if (context.Response.StatusCode == StatusCodes.Status204NoContent)
288298
{
299+
// Cancel current request to release any waiting poll and let dispose aquire the lock
300+
currentRequestTcs.TrySetCanceled();
301+
289302
// We should be able to safely dispose because there's no more data being written
290303
// We don't need to wait for close here since we've already waited for both sides
291304
await _manager.DisposeAndRemoveAsync(connection, closeGracefully: false);

0 commit comments

Comments
 (0)