Skip to content

Ensure JTF.SwitchToMainThreadAsync always throws when canceled - #576

Merged
Andrew Arnott (AArnott) merged 2 commits into
microsoft:masterfrom
AArnott:STMTA_Cancellation
Dec 20, 2019
Merged

Ensure JTF.SwitchToMainThreadAsync always throws when canceled#576
Andrew Arnott (AArnott) merged 2 commits into
microsoft:masterfrom
AArnott:STMTA_Cancellation

Conversation

@AArnott

Copy link
Copy Markdown
Member

Even if the caller is on the main thread or reaches the main thread before cancellation from a threadpool thread can be processed, callers tend to expect that the OperationCanceledException will be thrown regardless. So this changes behavior to meet expectations.

This also removes the VSTHRD201 analyzer recently added that helped people workaround the fact that this hadn't been the behavior. The analyzer is no longer necessary.

Closes #434

@AArnott

Copy link
Copy Markdown
Member Author

@sharwell Sam Harwell (sharwell) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This seems like intuitive behavior. We should take care to make the synchronization context behavior match what TaskAwaiter does so it's no more confusing than necessary.

Comment thread src/Microsoft.VisualStudio.Threading/JoinableTaskFactory.cs
Even if the caller is on the main thread or reaches the main thread before cancellation from a threadpool thread can be processed, callers tend to expect that the `OperationCanceledException` will be thrown regardless. So this changes behavior to meet expectations.

This also removes the VSTHRD201 analyzer recently added that helped people workaround the fact that this hadn't been the behavior. The analyzer is no longer necessary.

Closes microsoft#434
@AArnott

Copy link
Copy Markdown
Member Author

Sam Harwell (@sharwell) I know you already signed off, but since I ended up "not fixing" the issue you called out on the basis that I think it's behaving properly, I'd love to get your confirmation that you agree, or give you another chance to tell me why I'm wrong. :)

@sharwell

Copy link
Copy Markdown
Contributor

LGTM I just wanted to make sure the case was carefully considered. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Always throw from SwitchToMainThreadAsync if the token is canceled

2 participants