Propagate cancellation tokens to TrySetCanceled in Dataflow (#80978)
authorStephen Toub <stoub@microsoft.com>
Sat, 28 Jan 2023 16:04:49 +0000 (11:04 -0500)
committerGitHub <noreply@github.com>
Sat, 28 Jan 2023 16:04:49 +0000 (11:04 -0500)
commit73da129c973795f40252b0f47d8d52effddd5b4d
treeb2ae934cb15216284688cbb487375c04dedc0675
parentb6cd3003c05d9be521d6d1ffff9207255d177116
Propagate cancellation tokens to TrySetCanceled in Dataflow (#80978)

* Propagate cancellation tokens to TrySetCanceled in Dataflow

When the System.Threading.Tasks.Dataflow library was originally written, CancellationTokenSource's TrySetCanceled didn't have an overload that allowed passing in the CancellationToken that was the cause of the cancellation. Now it does, and we no longer build for target platforms that lack the needed overload.  Thus we can update the library to propagate it everywhere that's relevant.  In some cases, to do this well we do need to rely on a newer CancellationToken.Register overload that accepts a delegate which accepts a token, so there's a little bit of ifdef'ing involved still.

While doing this, I also took the opportunity to sprinkle some `static`s onto lambdas, since I was already doing so for some lambdas as part of this fix.

* Fix pipelines handling of cancellation token

Flush operations were synchronously throwing an exception if cancellation was requested prior to the operation.  Cancellation exceptions should always be propagated out through the returned task.

* Fix pre-cancellation in QuicStream.WriteAsync

Cancellation exceptions should flow out through the returned task, not synchronously.
39 files changed:
src/libraries/Common/tests/StreamConformanceTests/System/IO/StreamConformanceTests.cs
src/libraries/Common/tests/TestUtilities/System/AssertExtensions.cs
src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/Pipe.cs
src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/PipeAwaitable.cs
src/libraries/System.IO.Pipelines/tests/FlushAsyncCancellationTests.cs
src/libraries/System.Net.Quic/src/System/Net/Quic/QuicStream.cs
src/libraries/System.Threading.Channels/tests/BoundedChannelTests.cs
src/libraries/System.Threading.Channels/tests/ChannelTestBase.cs
src/libraries/System.Threading.Channels/tests/TestBase.cs
src/libraries/System.Threading.Tasks.Dataflow/src/Base/DataflowBlock.cs
src/libraries/System.Threading.Tasks.Dataflow/src/Blocks/ActionBlock.cs
src/libraries/System.Threading.Tasks.Dataflow/src/Blocks/BatchBlock.cs
src/libraries/System.Threading.Tasks.Dataflow/src/Blocks/BatchedJoinBlock.cs
src/libraries/System.Threading.Tasks.Dataflow/src/Blocks/BroadcastBlock.cs
src/libraries/System.Threading.Tasks.Dataflow/src/Blocks/BufferBlock.cs
src/libraries/System.Threading.Tasks.Dataflow/src/Blocks/JoinBlock.cs
src/libraries/System.Threading.Tasks.Dataflow/src/Blocks/TransformBlock.cs
src/libraries/System.Threading.Tasks.Dataflow/src/Blocks/TransformManyBlock.IAsyncEnumerable.cs
src/libraries/System.Threading.Tasks.Dataflow/src/Blocks/TransformManyBlock.cs
src/libraries/System.Threading.Tasks.Dataflow/src/Blocks/WriteOnceBlock.cs
src/libraries/System.Threading.Tasks.Dataflow/src/Internal/Common.cs
src/libraries/System.Threading.Tasks.Dataflow/src/Internal/DataflowEtwProvider.cs
src/libraries/System.Threading.Tasks.Dataflow/src/Internal/SourceCore.cs
src/libraries/System.Threading.Tasks.Dataflow/src/Internal/SpscTargetCore.cs
src/libraries/System.Threading.Tasks.Dataflow/src/Internal/TargetCore.cs
src/libraries/System.Threading.Tasks.Dataflow/tests/Dataflow/ActionBlockTests.cs
src/libraries/System.Threading.Tasks.Dataflow/tests/Dataflow/BatchBlockTests.cs
src/libraries/System.Threading.Tasks.Dataflow/tests/Dataflow/BatchedJoinBlockTests.cs
src/libraries/System.Threading.Tasks.Dataflow/tests/Dataflow/BroadcastBlockTests.cs
src/libraries/System.Threading.Tasks.Dataflow/tests/Dataflow/BufferBlockTests.cs
src/libraries/System.Threading.Tasks.Dataflow/tests/Dataflow/DataflowBlockExtensionTests.IAsyncEnumerable.cs
src/libraries/System.Threading.Tasks.Dataflow/tests/Dataflow/DataflowBlockExtensionTests.cs
src/libraries/System.Threading.Tasks.Dataflow/tests/Dataflow/DataflowTestHelper.cs
src/libraries/System.Threading.Tasks.Dataflow/tests/Dataflow/JoinBlockTests.cs
src/libraries/System.Threading.Tasks.Dataflow/tests/Dataflow/TransformBlockTests.cs
src/libraries/System.Threading.Tasks.Dataflow/tests/Dataflow/TransformManyBlockTests.IAsyncEnumerable.cs
src/libraries/System.Threading.Tasks.Dataflow/tests/Dataflow/TransformManyBlockTests.cs
src/libraries/System.Threading.Tasks.Dataflow/tests/Dataflow/WriteOnceBlockTests.cs
src/libraries/System.Threading.Tasks.Parallel/tests/ParallelForEachAsyncTests.cs