Fix corner-case handling of cancellation exception in ForEachAsync (#59065)
authorStephen Toub <stoub@microsoft.com>
Fri, 17 Sep 2021 05:17:35 +0000 (01:17 -0400)
committerGitHub <noreply@github.com>
Fri, 17 Sep 2021 05:17:35 +0000 (01:17 -0400)
commit53d1d2c6ec322fd5eb3351982107d389c182ad46
treef7d107cba62f10c7beb3146d5179e3039d8d3c15
parent3409f73c7893f863297e5eb5d17241dcf0796733
Fix corner-case handling of cancellation exception in ForEachAsync (#59065)

* Fix corner-case handling of cancellation exception in ForEachAsync

If code in Parallel.ForEachAsync throws OperationCanceledExceptions containing the CancellationToken passed to the iteration and that token has _not_ had cancellation requested (so why are they throwing with it) and there are no other exceptions, the ForEachAsync will effectively hang after failing to complete the task returned from it.

The issue stems from how we treat cancellation.  If the user-supplied token hasn't been canceled but we have OperationCanceledExceptions for the token passed into the iteration (the "internal" token), it can only have been canceled because an exception occurred.  We filter out these cancellation exceptions, leaving just the exceptions that are deemed to have caused the failure in the first place.  But the code doesn't currently account for the possibility that the developer is (arguably erroneously) throwing such an OperationCanceledException with the internal cancellation token as that root failure. The fix is to only filter out these OCEs if there are other exceptions besides them.

* Stop filtering out cancellation exceptions in Parallel.ForEachAsync
src/libraries/System.Threading.Tasks.Parallel/src/System/Threading/Tasks/Parallel.ForEachAsync.cs
src/libraries/System.Threading.Tasks.Parallel/tests/ParallelForEachAsyncTests.cs