How does a linked dataflow block cancel the target block

Clash Royale CLAN TAG#URR8PPPHow does a linked dataflow block cancel the target block
In TPL Dataflow, when a block is linked to another block with propagation, it will forward exceptions and also cancellations. I can imagine that forwarding an exception is simply done by using dataFlowBlock.Fault(exception), but I'm curious as to how the cancellation is forwarded as there is no such thing as dataFlowBlock.Cancel(). Is it done through the same Fault() method using the by passing TaskCancelledException as an argument?
dataFlowBlock.Fault(exception)
dataFlowBlock.Cancel()
Fault()
TaskCancelledException
Update:
To clarify, consider the following example where only block1 is created with a CancelationToken via options, and block2 isn't. Block1 is linked to block2 with propagation:
CancelationToken
block1 { CancellationToken = ct } -> block2 { }
When ct receives a cancelation request, block1 completion transitions to canceled. My question is what happens to block2 at this point? Does block1 actively cancel block2, and if so, does it do it using block.Fault(TaskCanceledException)? Or does it use some internal ocus-pocus that magically cancels block2 even though it was created without cancelation token?
block.Fault(TaskCanceledException)
BTW why ask about cancellation in the first place? Blocks shouldn't raise exceptions unless they've failed completely and the pipeline has to be torn down. They are meant to handle a stream of messages. Some will always fail and those could be wrapped and redirected eg to a logging component, using LinkTo with a predicate. They don't have to bring down the entire pipeline
– Panagiotis Kanavos
1 hour ago
1 Answer
1
Cancellation comes in the form of an option, CancellationToken, on ExecutionDataflowBlockOptions The token passed will complete the blocks successfully. What that means is the blocks will treat an OperationCanceledException differently than other exceptions thrown in the pipeline and cause the blocks to simply complete once done processing. Cancellation is intended to work by sharing a single CTS and using the associated tokens into the block options. Then when the CTS is cancelled all blocks get the cancellation signal.
CancellationToken
ExecutionDataflowBlockOptions
OperationCanceledException
Also more can be found here from MS.
Pointing to your actual question in the comments, this might explain things a little more.
When a dataflow block is canceled explicitly, the AggregateException object contains OperationCanceledException in the InnerExceptions property
Also:
The TPL provides a mechanism that enables tasks to coordinate cancellation in a cooperative manner. To enable dataflow blocks to participate in this cancellation mechanism, set the CancellationToken property. When this CancellationToken object is set to the canceled state, all dataflow blocks that monitor this token finish execution of their current item but do not start processing subsequent items. These dataflow blocks also clear any buffered messages, release connections to any source and target blocks, and transition to the canceled state. By transitioning to the canceled state, the Completion property has the Status property set to Canceled, unless an exception occurred during processing. In that case, Status is set to Faulted.
Source
This is a lot quoted text but the source link is provided and the explanations are well written.
So the blocks don't actively propagate cancellation. However, once the cancelled block finishes, with propagate to true, will flow that completion task right along with a cancelled or faulted state.
Thanks @JSteward, but perhaps my question wasn't clear, I understand how to create a "cancellable" block using the the options, my question is: regardless of the options used during block creation, when linking blocks with propagation, the source block somehow "gains" the ability to cancel the target block upon cancellation. I was wondering if this was done by faulting the target block with a
TaskCancelledException or through some other mechanism.– Luis Ferrao
yesterday
TaskCancelledException
Thanks for the feedback. Can you elaborate on this:` gains the ability to cancel the target block what exactly are you referring to, was this meant gains the ability to Complete the target block
– JSteward
yesterday
@JSteward Dataflow cancellation isn't related to CancellationToken or Task cancellation. Dataflow is a higher level library that handles linking and propagating completion from one source to one or more targets internally
– Panagiotis Kanavos
18 hours ago
@PanagiotisKanavos I'm not really following you. I can agree with the second sentence: high level library.... However, I don't see how
CancellationTokens and Task cancellation don't apply. After all you pass a cancellation token into the block and/or pipeline for the express purpose of canceling the block/pipeline. Maybe I'm missing something?– JSteward
16 hours ago
CancellationTokens
Task
@JSteward I think the confusion here is coming from the fact that we're all mixing active block cancelation with passive cancelation propagation. I'm going to update the question with a concrete example to specify exactly what piece of the puzzle I'm after, hopefully it'll clarify things.
– Luis Ferrao
3 mins ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You can actually check the code. When a source completes the library itself calls Fault or Complete on the linked targets. That's an implementation detail though
– Panagiotis Kanavos
18 hours ago