@@ -15,7 +15,7 @@ public static IAsyncEnumerable<TResponse> DefineStreamInvoke<TResponse, TRequest
1515 context ,
1616 eventDefinition ,
1717 cancellationToken ,
18- invokeId =>
18+ ( invokeId , _ ) =>
1919 {
2020 var sendEvent = new EventDefinition < SendPayload < TRequest > > ( eventDefinition . SendEventId ) ;
2121 context . Emit ( sendEvent , new SendPayload < TRequest > ( invokeId , request ) ) ;
@@ -37,29 +37,29 @@ public static IAsyncEnumerable<TResponse> DefineStreamInvoke<TResponse, TRequest
3737 context ,
3838 eventDefinition ,
3939 cancellationToken ,
40- async invokeId =>
40+ async ( invokeId , requestCancellationToken ) =>
4141 {
4242 var sendEvent = new EventDefinition < SendPayload < TRequest > > ( eventDefinition . SendEventId ) ;
4343 var sendStreamEndEvent = new EventDefinition < StreamEndPayload > ( eventDefinition . SendStreamEndId ) ;
4444
4545 try
4646 {
47- await foreach ( var item in request . WithCancellation ( cancellationToken ) . ConfigureAwait ( false ) )
47+ await foreach ( var item in request . WithCancellation ( requestCancellationToken ) . ConfigureAwait ( false ) )
4848 {
49- if ( cancellationToken . IsCancellationRequested )
49+ if ( requestCancellationToken . IsCancellationRequested )
5050 {
5151 return ;
5252 }
5353
5454 context . Emit ( sendEvent , new SendPayload < TRequest > ( invokeId , item ) ) ;
5555 }
5656 }
57- catch ( OperationCanceledException ) when ( cancellationToken . IsCancellationRequested )
57+ catch ( OperationCanceledException ) when ( requestCancellationToken . IsCancellationRequested )
5858 {
5959 return ;
6060 }
6161
62- if ( ! cancellationToken . IsCancellationRequested )
62+ if ( ! requestCancellationToken . IsCancellationRequested )
6363 {
6464 context . Emit ( sendStreamEndEvent , new StreamEndPayload ( invokeId ) ) ;
6565 }
@@ -109,9 +109,7 @@ async Task HandleInvokeAsync(string invokeId, TRequest request)
109109 context . Emit ( receiveStreamEndEvent , new StreamEndPayload ( invokeId ) ) ;
110110 }
111111 }
112- catch ( OperationCanceledException ) when ( cancellationSource . IsCancellationRequested )
113- {
114- }
112+ catch ( OperationCanceledException ) when ( cancellationSource . IsCancellationRequested ) { }
115113 catch ( Exception error )
116114 {
117115 if ( ! cancellationSource . IsCancellationRequested )
@@ -220,9 +218,7 @@ async Task HandleInvokeAsync(RequestStreamInvocationState<TRequest> state)
220218 context . Emit ( receiveStreamEndEvent , new StreamEndPayload ( state . InvokeId ) ) ;
221219 }
222220 }
223- catch ( OperationCanceledException ) when ( state . CancellationSource . IsCancellationRequested )
224- {
225- }
221+ catch ( OperationCanceledException ) when ( state . CancellationSource . IsCancellationRequested ) { }
226222 catch ( Exception error )
227223 {
228224 if ( ! state . CancellationSource . IsCancellationRequested )
@@ -319,14 +315,18 @@ private static IAsyncEnumerable<TResponse> CreateStreamInvoke<TResponse, TReques
319315 IEventContext context ,
320316 InvokeEventDefinition < TResponse , TRequest > eventDefinition ,
321317 CancellationToken cancellationToken ,
322- Func < string , Task > sendRequest )
318+ Func < string , CancellationToken , Task > sendRequest )
323319 {
324320 var invokeId = IdGenerator . New ( ) ;
325321 var sendAbortEvent = new EventDefinition < AbortPayload > ( eventDefinition . SendAbortId ) ;
326322 var receiveEvent = new EventDefinition < ReceivePayload < TResponse > > ( eventDefinition . ReceiveEventId ) ;
327323 var receiveErrorEvent = new EventDefinition < ReceiveErrorPayload > ( eventDefinition . ReceiveErrorId ) ;
328324 var receiveStreamEndEvent = new EventDefinition < StreamEndPayload > ( eventDefinition . ReceiveStreamEndId ) ;
329325 var responses = new AsyncSignalQueue < TResponse > ( ) ;
326+ var requestCancellationSource = cancellationToken . CanBeCanceled
327+ ? CancellationTokenSource . CreateLinkedTokenSource ( cancellationToken )
328+ : new CancellationTokenSource ( ) ;
329+ var requestCancellationToken = requestCancellationSource . Token ;
330330 var subscriptions = new List < IDisposable > ( ) ;
331331 var finished = 0 ;
332332
@@ -338,37 +338,20 @@ void Cleanup()
338338 }
339339 }
340340
341- void Complete ( )
342- {
343- if ( Interlocked . Exchange ( ref finished , 1 ) != 0 )
344- {
345- return ;
346- }
347-
348- responses . Complete ( ) ;
349- Cleanup ( ) ;
350- }
351-
352- void Fault ( Exception error )
341+ void Finish ( Exception ? error , bool emitAbort )
353342 {
354343 if ( Interlocked . Exchange ( ref finished , 1 ) != 0 )
355344 {
356345 return ;
357346 }
358347
359- responses . Fault ( error ) ;
360- Cleanup ( ) ;
361- }
348+ requestCancellationSource . Cancel ( ) ;
362349
363- void AbortWithCompletion ( Exception ? error )
364- {
365- if ( Interlocked . Exchange ( ref finished , 1 ) != 0 )
350+ if ( emitAbort )
366351 {
367- return ;
352+ context . Emit ( sendAbortEvent , new AbortPayload ( invokeId ) ) ;
368353 }
369354
370- context . Emit ( sendAbortEvent , new AbortPayload ( invokeId ) ) ;
371-
372355 if ( error is null )
373356 {
374357 responses . Complete ( ) ;
@@ -379,6 +362,22 @@ void AbortWithCompletion(Exception? error)
379362 }
380363
381364 Cleanup ( ) ;
365+ requestCancellationSource . Dispose ( ) ;
366+ }
367+
368+ void Complete ( )
369+ {
370+ Finish ( error : null , emitAbort : false ) ;
371+ }
372+
373+ void Fault ( Exception error )
374+ {
375+ Finish ( error , emitAbort : false ) ;
376+ }
377+
378+ void AbortWithCompletion ( Exception ? error )
379+ {
380+ Finish ( error , emitAbort : true ) ;
382381 }
383382
384383 subscriptions . Add ( context . On ( receiveEvent , envelope =>
@@ -428,11 +427,9 @@ void AbortWithCompletion(Exception? error)
428427 {
429428 try
430429 {
431- await sendRequest ( invokeId ) . ConfigureAwait ( false ) ;
432- }
433- catch ( OperationCanceledException ) when ( cancellationToken . IsCancellationRequested )
434- {
430+ await sendRequest ( invokeId , requestCancellationToken ) . ConfigureAwait ( false ) ;
435431 }
432+ catch ( OperationCanceledException ) when ( requestCancellationToken . IsCancellationRequested ) { }
436433 catch ( Exception error )
437434 {
438435 Fault ( error ) ;
0 commit comments