@@ -20,7 +20,6 @@ public abstract partial class Request<TOptions, TCompleted, TFailed> : IRequest,
2020 private CancellationTokenRegistration _ctr ;
2121
2222 private TaskCompletionSource < bool > ? _pauseTcs ;
23- private volatile bool _hasPausedExecution ;
2423
2524 private readonly RequestStateMachine _stateMachine ;
2625
@@ -291,27 +290,21 @@ async Task IRequest.StartRequestAsync()
291290 return ;
292291
293292 // Check if we're resuming a paused execution
294- if ( _hasPausedExecution )
293+ TaskCompletionSource < bool > ? tcs = Interlocked . Exchange ( ref _pauseTcs , null ) ;
294+ if ( tcs != null )
295295 {
296- // Get and clear the pause TCS atomically
297- TaskCompletionSource < bool > ? tcs = Interlocked . Exchange ( ref _pauseTcs , null ) ;
298- if ( tcs != null )
299- {
300- _hasPausedExecution = false ;
301-
302- if ( ! _stateMachine . TryTransition ( RequestState . Running ) )
303- return ;
296+ if ( ! _stateMachine . TryTransition ( RequestState . Running ) )
297+ return ;
304298
305- _runningSourceVersion ++ ;
306- _runningSource . Reset ( ) ;
299+ _runningSourceVersion ++ ;
300+ _runningSource . Reset ( ) ;
307301
308- // Resume the paused execution
309- tcs . TrySetResult ( true ) ;
302+ // Resume the paused execution
303+ tcs . TrySetResult ( true ) ;
310304
311- // Wait for the resumed execution to stop running
312- await new ValueTask ( this , _runningSourceVersion ) . ConfigureAwait ( false ) ;
313- return ;
314- }
305+ // Wait for the resumed execution to stop running
306+ await new ValueTask ( this , _runningSourceVersion ) . ConfigureAwait ( false ) ;
307+ return ;
315308 }
316309
317310 if ( ! _stateMachine . TryTransition ( RequestState . Running ) )
@@ -492,10 +485,8 @@ private async ValueTask YieldAsyncSlow()
492485
493486 if ( State == RequestState . Paused )
494487 {
495- // Lazy-create pause TCS only when actually pausing
496- TaskCompletionSource < bool > tcs = new ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
497- _pauseTcs = tcs ;
498- _hasPausedExecution = true ;
488+ TaskCompletionSource < bool > tcs = new TaskCompletionSource < bool > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
489+ tcs = Interlocked . CompareExchange ( ref _pauseTcs , tcs , null ) ?? tcs ;
499490
500491 // Wait for resume
501492 await tcs . Task . ConfigureAwait ( false ) ;
@@ -573,7 +564,7 @@ public virtual void Dispose()
573564 _requestCts ? . Dispose ( ) ;
574565 _ctr . Dispose ( ) ;
575566 _completionSource . TrySetCanceled ( ) ;
576- _pauseTcs ? . TrySetCanceled ( ) ;
567+ Interlocked . Exchange ( ref _pauseTcs , null ) ? . TrySetCanceled ( ) ;
577568
578569 GC . SuppressFinalize ( this ) ;
579570 }
0 commit comments