Skip to content

Commit

Permalink
Cancel previous request on close (#2923)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennanConroy authored Sep 10, 2018
1 parent 753c3da commit 399ac26
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,18 @@ private async Task ExecuteAsync(HttpContext context, ConnectionDelegate connecti
// Cancel the previous request
connection.Cancellation?.Cancel();

// Wait for the previous request to drain
await connection.PreviousPollTask;
try
{
// Wait for the previous request to drain
await connection.PreviousPollTask;
}
catch (OperationCanceledException)
{
// Previous poll canceled due to connection closing, close this poll too
context.Response.ContentType = "text/plain";
context.Response.StatusCode = StatusCodes.Status204NoContent;
return;
}

connection.PreviousPollTask = currentRequestTcs.Task;
}
Expand Down Expand Up @@ -286,6 +296,9 @@ private async Task ExecuteAsync(HttpContext context, ConnectionDelegate connecti
// If the status code is a 204 it means the connection is done
if (context.Response.StatusCode == StatusCodes.Status204NoContent)
{
// Cancel current request to release any waiting poll and let dispose aquire the lock
currentRequestTcs.TrySetCanceled();

// We should be able to safely dispose because there's no more data being written
// We don't need to wait for close here since we've already waited for both sides
await _manager.DisposeAndRemoveAsync(connection, closeGracefully: false);
Expand Down

0 comments on commit 399ac26

Please sign in to comment.