@@ -1485,8 +1485,7 @@ [new RequestMessage(1, ClientAttachDebuggerMethod, null), TestContext.Cancellati
14851485 out QueueingSynchronizationContext context ) ;
14861486
14871487 handlerCompletion . TrySetResult ( new Dictionary < string , object ? > { [ "success" ] = true } ) ;
1488- bool completedWithoutPumping = await CompletesQuicklyAsync ( invocation ) . ConfigureAwait ( false ) ;
1489- DrainContextUntilCompleted ( context , invocation ) ;
1488+ bool completedWithoutPumping = await CompletesWithoutPumpingAsync ( invocation , context ) . ConfigureAwait ( false ) ;
14901489 _ = await invocation . ConfigureAwait ( false ) ;
14911490
14921491 Assert . IsTrue ( completedWithoutPumping , "Awaiting the public server-request handler must not capture the calling synchronization context." ) ;
@@ -1708,8 +1707,7 @@ public async Task JsonRpcConnection_RequestAwaits_DoNotCaptureTheCallingSynchron
17081707 writeCompletion . TrySetResult ( true ) ;
17091708 readCompletion . TrySetResult ( new ResponseMessage ( 1 , new Dictionary < string , object ? > ( ) ) ) ;
17101709
1711- bool completedWithoutPumping = await CompletesQuicklyAsync ( request ) . ConfigureAwait ( false ) ;
1712- DrainContextUntilCompleted ( context , request ) ;
1710+ bool completedWithoutPumping = await CompletesWithoutPumpingAsync ( request , context ) . ConfigureAwait ( false ) ;
17131711 _ = await request . ConfigureAwait ( false ) ;
17141712
17151713 Assert . IsTrue ( completedWithoutPumping , "Request continuations must not be posted to the caller's synchronization context." ) ;
@@ -1736,8 +1734,7 @@ public async Task JsonRpcConnection_WriteLockAwait_DoesNotCaptureTheCallingSynch
17361734 out QueueingSynchronizationContext context ) ;
17371735 firstWriteCompletion . TrySetResult ( true ) ;
17381736
1739- bool completedWithoutPumping = await CompletesQuicklyAsync ( second ) . ConfigureAwait ( false ) ;
1740- DrainContextUntilCompleted ( context , second ) ;
1737+ bool completedWithoutPumping = await CompletesWithoutPumpingAsync ( second , context ) . ConfigureAwait ( false ) ;
17411738 await Task . WhenAll ( first , second ) . ConfigureAwait ( false ) ;
17421739
17431740 Assert . IsTrue ( completedWithoutPumping , "Waiting for the write lock must not post to the caller's synchronization context." ) ;
@@ -1764,7 +1761,7 @@ public async Task JsonRpcConnection_ReadLoop_DoesNotCaptureAHandlerSynchronizati
17641761
17651762 readCompletion . TrySetResult ( new NotificationMessage ( "notification" , null ) ) ;
17661763
1767- bool completedWithoutPumping = await CompletesQuicklyAsync ( notificationReceived . Task ) . ConfigureAwait ( false ) ;
1764+ bool completedWithoutPumping = await CompletesWithoutPumpingAsync ( notificationReceived . Task , context ) . ConfigureAwait ( false ) ;
17681765 DrainContextUntilCompleted ( context , readLoop ) ;
17691766 await WithTimeoutAsync ( readLoop ) . ConfigureAwait ( false ) ;
17701767 Assert . IsTrue ( completedWithoutPumping , "The read loop must not post message dispatch to a handler-provided synchronization context." ) ;
@@ -1791,8 +1788,7 @@ public async Task JsonRpcConnection_ServerRequestHandlerAwait_DoesNotCaptureTheC
17911788 out QueueingSynchronizationContext context ) ;
17921789
17931790 handlerCompletion . TrySetResult ( new Dictionary < string , object ? > { [ "success" ] = true } ) ;
1794- bool completedWithoutPumping = await CompletesQuicklyAsync ( request ) . ConfigureAwait ( false ) ;
1795- DrainContextUntilCompleted ( context , request ) ;
1791+ bool completedWithoutPumping = await CompletesWithoutPumpingAsync ( request , context ) . ConfigureAwait ( false ) ;
17961792 await request . ConfigureAwait ( false ) ;
17971793
17981794 Assert . IsTrue ( completedWithoutPumping , "Awaiting the server-request handler must not capture the calling synchronization context." ) ;
@@ -1820,8 +1816,7 @@ public async Task JsonRpcConnection_ServerResponseWriteAwait_DoesNotCaptureTheCa
18201816 await WithTimeoutAsync ( handler . WriteStarted ) . ConfigureAwait ( false ) ;
18211817
18221818 writeCompletion . TrySetResult ( true ) ;
1823- bool completedWithoutPumping = await CompletesQuicklyAsync ( request ) . ConfigureAwait ( false ) ;
1824- DrainContextUntilCompleted ( context , request ) ;
1819+ bool completedWithoutPumping = await CompletesWithoutPumpingAsync ( request , context ) . ConfigureAwait ( false ) ;
18251820 await request . ConfigureAwait ( false ) ;
18261821
18271822 Assert . IsTrue ( completedWithoutPumping , "Awaiting the server response write must not capture the calling synchronization context." ) ;
@@ -1842,8 +1837,7 @@ public async Task JsonRpcConnection_CancelNotificationAwait_DoesNotCaptureTheHan
18421837 await WithTimeoutAsync ( handler . WriteStarted ) . ConfigureAwait ( false ) ;
18431838
18441839 cancelWriteCompletion . TrySetResult ( true ) ;
1845- bool completedWithoutPumping = await CompletesQuicklyAsync ( cancel ) . ConfigureAwait ( false ) ;
1846- DrainContextUntilCompleted ( context , cancel ) ;
1840+ bool completedWithoutPumping = await CompletesWithoutPumpingAsync ( cancel , context ) . ConfigureAwait ( false ) ;
18471841 await cancel . ConfigureAwait ( false ) ;
18481842
18491843 Assert . IsTrue ( completedWithoutPumping , "Awaiting the cancel notification write must not capture the calling synchronization context." ) ;
@@ -2165,8 +2159,7 @@ private static async Task<TException> AssertThrowsAsync<TException>(Func<Task> a
21652159 private static async Task < T > AssertCompletesWithoutPumpingAsync < T > ( Func < Task < T > > action )
21662160 {
21672161 Task < T > task = InvokeWithSynchronizationContext ( action , out QueueingSynchronizationContext context ) ;
2168- bool completedWithoutPumping = await CompletesQuicklyAsync ( task ) . ConfigureAwait ( false ) ;
2169- DrainContextUntilCompleted ( context , task ) ;
2162+ bool completedWithoutPumping = await CompletesWithoutPumpingAsync ( task , context ) . ConfigureAwait ( false ) ;
21702163 T result = await task . ConfigureAwait ( false ) ;
21712164
21722165 Assert . IsTrue (
@@ -2178,8 +2171,7 @@ private static async Task<T> AssertCompletesWithoutPumpingAsync<T>(Func<Task<T>>
21782171 private static async Task AssertCompletesWithoutPumpingAsync ( Func < Task > action )
21792172 {
21802173 Task task = InvokeWithSynchronizationContext ( action , out QueueingSynchronizationContext context ) ;
2181- bool completedWithoutPumping = await CompletesQuicklyAsync ( task ) . ConfigureAwait ( false ) ;
2182- DrainContextUntilCompleted ( context , task ) ;
2174+ bool completedWithoutPumping = await CompletesWithoutPumpingAsync ( task , context ) . ConfigureAwait ( false ) ;
21832175 await task . ConfigureAwait ( false ) ;
21842176
21852177 Assert . IsTrue (
@@ -2221,8 +2213,20 @@ private static Task InvokeWithSynchronizationContext(
22212213 }
22222214 }
22232215
2224- private static async Task < bool > CompletesQuicklyAsync ( Task task )
2225- => await Task . WhenAny ( task , Task . Delay ( TimeSpan . FromSeconds ( 1 ) ) ) . ConfigureAwait ( false ) == task ;
2216+ private static async Task < bool > CompletesWithoutPumpingAsync ( Task task , QueueingSynchronizationContext context )
2217+ {
2218+ try
2219+ {
2220+ Task completed = await WithTimeoutAsync ( Task . WhenAny ( task , context . ContinuationPosted ) ) . ConfigureAwait ( false ) ;
2221+ return completed == task && context . PostCount == 0 ;
2222+ }
2223+ finally
2224+ {
2225+ DrainContextUntilCompleted ( context , task ) ;
2226+ // Join completion without rethrowing faults that the caller asserts separately.
2227+ _ = await WithTimeoutAsync ( Task . WhenAny ( task ) ) . ConfigureAwait ( false ) ;
2228+ }
2229+ }
22262230
22272231 private static void DrainContextUntilCompleted ( QueueingSynchronizationContext context , Task task )
22282232 {
@@ -2293,17 +2297,22 @@ private sealed class QueueingSynchronizationContext : SynchronizationContext
22932297#else
22942298 private readonly object _lock = new ( ) ;
22952299#endif
2300+ private readonly TaskCompletionSource < bool > _continuationPosted = new ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
22962301 private int _postCount ;
22972302
22982303 public int PostCount => Volatile . Read ( ref _postCount ) ;
22992304
2305+ public Task ContinuationPosted => _continuationPosted . Task ;
2306+
23002307 public override void Post ( SendOrPostCallback d , object ? state )
23012308 {
23022309 _ = Interlocked . Increment ( ref _postCount ) ;
23032310 lock ( _lock )
23042311 {
23052312 _callbacks . Enqueue ( ( d , state ) ) ;
23062313 }
2314+
2315+ _continuationPosted . TrySetResult ( true ) ;
23072316 }
23082317
23092318 public bool TryRunOne ( )
0 commit comments