Skip to content

Fix cancellation handling in Parallel.For#48056

Merged
sharwell merged 2 commits intodotnet:masterfrom
sharwell:aggregate-cancellation
Sep 28, 2020
Merged

Fix cancellation handling in Parallel.For#48056
sharwell merged 2 commits intodotnet:masterfrom
sharwell:aggregate-cancellation

Conversation

@sharwell
Copy link
Contributor

This change ensures that cancellation exceptions that get wrapped in an AggregateException are treated as cancellation instead of failure.

This fixes the error that resulted in a failure of NotNullIfNotNull_Return_DuplicateParameters in Pipelines - Run 20200925.9 (azure.com).

@sharwell sharwell requested a review from a team as a code owner September 25, 2020 18:22
@sharwell
Copy link
Contributor Author

@dotnet/roslyn-compiler for a second review

This change ensures that cancellation exceptions that get wrapped in an
AggregateException are treated as cancellation instead of failure.
@sharwell sharwell force-pushed the aggregate-cancellation branch from dfa9711 to 89567d8 Compare September 26, 2020 00:11
catch (OperationCanceledException e) when (cancellationToken.IsCancellationRequested && e.CancellationToken != cancellationToken)
{
// Parallel.For checks for a specific cancellation token, so make sure we throw with the
// correct one.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious: how did you discover this behavior? If there is a doc you read may be helpful to link to it here for future readers to find.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at referencesource.microsoft.com after observing a test failure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@sharwell sharwell Sep 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this policy is slightly too aggressive. The API would be easier to use if it supported inner cancellation exceptions with any cancellation token provided the one passed in the parallel options was also cancelled. But we just don't use these APIs enough to figure out all the details on that.

@sharwell sharwell merged commit ac4af9e into dotnet:master Sep 28, 2020
@sharwell sharwell deleted the aggregate-cancellation branch September 28, 2020 14:50
@ghost ghost added this to the Next milestone Sep 28, 2020
@Cosifne Cosifne modified the milestones: Next, 16.9.P1 Oct 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants