@@ -40,7 +40,7 @@ internal class Http3Connection : IHttp3StreamLifetimeHandler, IRequestProcessor
40
40
private int _stoppedAcceptingStreams ;
41
41
private bool _gracefulCloseStarted ;
42
42
private int _activeRequestCount ;
43
- private readonly CancellationTokenSource _serverCloseCts ;
43
+ private readonly CancellationTokenSource _allRequestsCompleted ;
44
44
private readonly Http3PeerSettings _serverSettings = new Http3PeerSettings ( ) ;
45
45
private readonly Http3PeerSettings _clientSettings = new Http3PeerSettings ( ) ;
46
46
private readonly StreamCloseAwaitable _streamCompletionAwaitable = new StreamCloseAwaitable ( ) ;
@@ -51,7 +51,7 @@ public Http3Connection(HttpMultiplexedConnectionContext context)
51
51
_multiplexedContext = ( MultiplexedConnectionContext ) context . ConnectionContext ;
52
52
_context = context ;
53
53
_streamLifetimeHandler = this ;
54
- _serverCloseCts = new CancellationTokenSource ( ) ;
54
+ _allRequestsCompleted = new CancellationTokenSource ( ) ;
55
55
56
56
_errorCodeFeature = context . ConnectionFeatures . Get < IProtocolErrorCodeFeature > ( ) ! ;
57
57
@@ -237,7 +237,7 @@ public async Task ProcessRequestsAsync<TContext>(IHttpApplication<TContext> appl
237
237
238
238
while ( true )
239
239
{
240
- var streamContext = await _multiplexedContext . AcceptAsync ( _serverCloseCts . Token ) ;
240
+ var streamContext = await _multiplexedContext . AcceptAsync ( _allRequestsCompleted . Token ) ;
241
241
242
242
try
243
243
{
@@ -378,20 +378,14 @@ public async Task ProcessRequestsAsync<TContext>(IHttpApplication<TContext> appl
378
378
}
379
379
catch
380
380
{
381
- Abort ( ResolveConnectionError ( error ) , Http3ErrorCode . InternalError ) ;
381
+ Abort ( connectionError , Http3ErrorCode . InternalError ) ;
382
382
throw ;
383
383
}
384
384
finally
385
385
{
386
386
Log . Http3ConnectionClosed ( _context . ConnectionId , GetHighestStreamId ( ) ) ;
387
387
}
388
388
}
389
-
390
- static ConnectionAbortedException ResolveConnectionError ( Exception ? error )
391
- {
392
- return error as ConnectionAbortedException
393
- ?? new ConnectionAbortedException ( CoreStrings . Http3ConnectionFaulted , error ! ) ;
394
- }
395
389
}
396
390
397
391
private Http3StreamContext CreateHttpStreamContext ( ConnectionContext streamContext )
@@ -583,9 +577,14 @@ void IHttp3StreamLifetimeHandler.OnStreamCompleted(IHttp3Stream stream)
583
577
}
584
578
_streams . Remove ( stream . StreamId ) ;
585
579
586
- if ( _gracefulCloseStarted )
580
+ if ( stream . IsRequestStream )
587
581
{
588
- _serverCloseCts . Cancel ( ) ;
582
+ // Connection is gracefully closing and this is the final request.
583
+ // Trigger cancellation token to cause AcceptAsync to exit.
584
+ if ( _gracefulCloseStarted && _activeRequestCount == 0 )
585
+ {
586
+ _allRequestsCompleted . Cancel ( ) ;
587
+ }
589
588
}
590
589
}
591
590
0 commit comments