diff --git a/src/Servers/HttpSys/test/FunctionalTests/ResponseSendFileTests.cs b/src/Servers/HttpSys/test/FunctionalTests/ResponseSendFileTests.cs index 1bda0ddee1..8d54a67a53 100644 --- a/src/Servers/HttpSys/test/FunctionalTests/ResponseSendFileTests.cs +++ b/src/Servers/HttpSys/test/FunctionalTests/ResponseSendFileTests.cs @@ -487,17 +487,21 @@ namespace Microsoft.AspNetCore.Server.HttpSys try { + // Note Response.SendFileAsync uses RequestAborted by default. This can cause the response to be disposed + // before it throws an IOException, but there's a race depending on when the disconnect is noticed. + // Passing our own token to skip that. + using var cts = new CancellationTokenSource(); await Assert.ThrowsAsync(async () => { // It can take several tries before Send notices the disconnect. for (int i = 0; i < Utilities.WriteRetryLimit; i++) { - await httpContext.Response.SendFileAsync(AbsoluteFilePath, 0, null); + await httpContext.Response.SendFileAsync(AbsoluteFilePath, 0, null, cts.Token); } }); await Assert.ThrowsAsync(() => - httpContext.Response.SendFileAsync(AbsoluteFilePath, 0, null)); + httpContext.Response.SendFileAsync(AbsoluteFilePath, 0, null, cts.Token)); testComplete.SetResult(0); } @@ -573,9 +577,13 @@ namespace Microsoft.AspNetCore.Server.HttpSys var testComplete = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); using (Utilities.CreateHttpServer(out var address, async httpContext => { + // Note Response.SendFileAsync uses RequestAborted by default. This can cause the response to be disposed + // before it throws an IOException, but there's a race depending on when the disconnect is noticed. + // Passing our own token to skip that. + using var cts = new CancellationTokenSource(); httpContext.RequestAborted.Register(() => cancellationReceived.SetResult(0)); // First write sends headers - await httpContext.Response.SendFileAsync(AbsoluteFilePath, 0, null); + await httpContext.Response.SendFileAsync(AbsoluteFilePath, 0, null, cts.Token); firstSendComplete.SetResult(0); await clientDisconnected.Task; @@ -586,7 +594,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys // It can take several tries before Write notices the disconnect. for (int i = 0; i < Utilities.WriteRetryLimit; i++) { - await httpContext.Response.SendFileAsync(AbsoluteFilePath, 0, null, CancellationToken.None); + await httpContext.Response.SendFileAsync(AbsoluteFilePath, 0, null, cts.Token); } });