@@ -487,17 +487,21 @@ public async Task ResponseSendFileExceptions_ClientDisconnectsBeforeFirstSend_Se
487
487
488
488
try
489
489
{
490
+ // Note Response.SendFileAsync uses RequestAborted by default. This can cause the response to be disposed
491
+ // before it throws an IOException, but there's a race depending on when the disconnect is noticed.
492
+ // Passing our own token to skip that.
493
+ using var cts = new CancellationTokenSource ( ) ;
490
494
await Assert . ThrowsAsync < IOException > ( async ( ) =>
491
495
{
492
496
// It can take several tries before Send notices the disconnect.
493
497
for ( int i = 0 ; i < Utilities . WriteRetryLimit ; i ++ )
494
498
{
495
- await httpContext . Response . SendFileAsync ( AbsoluteFilePath , 0 , null ) ;
499
+ await httpContext . Response . SendFileAsync ( AbsoluteFilePath , 0 , null , cts . Token ) ;
496
500
}
497
501
} ) ;
498
502
499
503
await Assert . ThrowsAsync < ObjectDisposedException > ( ( ) =>
500
- httpContext . Response . SendFileAsync ( AbsoluteFilePath , 0 , null ) ) ;
504
+ httpContext . Response . SendFileAsync ( AbsoluteFilePath , 0 , null , cts . Token ) ) ;
501
505
502
506
testComplete . SetResult ( 0 ) ;
503
507
}
@@ -573,9 +577,13 @@ public async Task ResponseSendFileExceptions_ClientDisconnectsBeforeSecondSend_S
573
577
var testComplete = new TaskCompletionSource < int > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
574
578
using ( Utilities . CreateHttpServer ( out var address , async httpContext =>
575
579
{
580
+ // Note Response.SendFileAsync uses RequestAborted by default. This can cause the response to be disposed
581
+ // before it throws an IOException, but there's a race depending on when the disconnect is noticed.
582
+ // Passing our own token to skip that.
583
+ using var cts = new CancellationTokenSource ( ) ;
576
584
httpContext . RequestAborted . Register ( ( ) => cancellationReceived . SetResult ( 0 ) ) ;
577
585
// First write sends headers
578
- await httpContext . Response . SendFileAsync ( AbsoluteFilePath , 0 , null ) ;
586
+ await httpContext . Response . SendFileAsync ( AbsoluteFilePath , 0 , null , cts . Token ) ;
579
587
firstSendComplete . SetResult ( 0 ) ;
580
588
await clientDisconnected . Task ;
581
589
@@ -586,7 +594,7 @@ await Assert.ThrowsAsync<IOException>(async () =>
586
594
// It can take several tries before Write notices the disconnect.
587
595
for ( int i = 0 ; i < Utilities . WriteRetryLimit ; i ++ )
588
596
{
589
- await httpContext . Response . SendFileAsync ( AbsoluteFilePath , 0 , null , CancellationToken . None ) ;
597
+ await httpContext . Response . SendFileAsync ( AbsoluteFilePath , 0 , null , cts . Token ) ;
590
598
}
591
599
} ) ;
592
600
0 commit comments