@@ -404,9 +404,11 @@ public async Task StreamPool_SingleStream_ReturnedToPool()
404404 {
405405 // Add stream to Http2Connection._completedStreams inline with SetResult().
406406 var serverTcs = new TaskCompletionSource ( ) ;
407+ var appTcs = new TaskCompletionSource ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
407408
408409 await InitializeConnectionAsync ( async context =>
409410 {
411+ appTcs . TrySetResult ( ) ;
410412 await serverTcs . Task ;
411413 await _echoApplication ( context ) ;
412414 } ) ;
@@ -415,6 +417,8 @@ await InitializeConnectionAsync(async context =>
415417
416418 await StartStreamAsync ( 1 , _browserRequestHeaders , endStream : true ) ;
417419
420+ // If app code is running we know the stream has been created
421+ await appTcs . Task ;
418422 var stream = _connection . _streams [ 1 ] ;
419423 serverTcs . SetResult ( ) ;
420424
@@ -490,9 +494,12 @@ public async Task StreamPool_MultipleStreamsInSequence_PooledStreamReused()
490494 TaskCompletionSource appDelegateTcs = null ;
491495 object persistedState = null ;
492496 var requestCount = 0 ;
497+ var appTcs = new TaskCompletionSource ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
493498
494499 await InitializeConnectionAsync ( async context =>
495500 {
501+ appTcs . TrySetResult ( ) ;
502+
496503 requestCount ++ ;
497504 var persistentStateCollection = context . Features . Get < IPersistentStateFeature > ( ) . State ;
498505 if ( persistentStateCollection . TryGetValue ( "Counter" , out var value ) )
@@ -509,6 +516,9 @@ await InitializeConnectionAsync(async context =>
509516 appDelegateTcs = new TaskCompletionSource ( ) ;
510517 await StartStreamAsync ( 1 , _browserRequestHeaders , endStream : true ) ;
511518
519+ // If app code is running we know the stream has been created
520+ await appTcs . Task ;
521+ appTcs = new TaskCompletionSource ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
512522 // Get the in progress stream
513523 var stream = _connection . _streams [ 1 ] ;
514524
@@ -534,6 +544,8 @@ await ExpectAsync(Http2FrameType.HEADERS,
534544 appDelegateTcs = new TaskCompletionSource ( ) ;
535545 await StartStreamAsync ( 3 , _browserRequestHeaders , endStream : true ) ;
536546
547+ await appTcs . Task ;
548+
537549 // New stream has been taken from the pool
538550 Assert . Equal ( 0 , _connection . StreamPool . Count ) ;
539551
@@ -564,9 +576,11 @@ public async Task StreamPool_StreamIsInvalidState_DontReturnedToPool()
564576 {
565577 // Add (or don't add) stream to Http2Connection._completedStreams inline with SetResult().
566578 var serverTcs = new TaskCompletionSource ( ) ;
579+ var appTcs = new TaskCompletionSource ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
567580
568581 await InitializeConnectionAsync ( async context =>
569582 {
583+ appTcs . TrySetResult ( ) ;
570584 await serverTcs . Task . DefaultTimeout ( ) ;
571585
572586 await context . Response . WriteAsync ( "Content" ) ;
@@ -575,6 +589,8 @@ await InitializeConnectionAsync(async context =>
575589
576590 await StartStreamAsync ( 1 , _browserRequestHeaders , endStream : true ) ;
577591
592+ // If app code is running we know the stream has been created
593+ await appTcs . Task ;
578594 var stream = _connection . _streams [ 1 ] ;
579595 serverTcs . SetResult ( ) ;
580596
0 commit comments