Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fix VSTHRD003 to allow for awaiting more Task properties - #1252

#1252
Merged
AArnott merged 1 commit into
microsoft:mainmicrosoft/vs-threading:mainfrom
AArnott:analyzerFixesAArnott/vs-threading:analyzerFixesCopy head branch name to clipboard
Oct 25, 2023
Merged

Fix VSTHRD003 to allow for awaiting more Task properties#1252
AArnott merged 1 commit into
microsoft:mainmicrosoft/vs-threading:mainfrom
AArnott:analyzerFixesAArnott/vs-threading:analyzerFixesCopy head branch name to clipboard

Conversation

@AArnott

@AArnott AArnott commented Oct 25, 2023

Copy link
Copy Markdown
Member

When integrating into the VS repo, recent enhancements to VSTHRD003 made insertion of the new analyzers problematic. In particular, two patterns were quite common and arguably should be allowed:

  • Awaiting on the JoinableTask.Task property on the result of a JoinableTaskFactory.RunAsync method call. In general, if a method returns an object, awaiting one of that object's properties might not be fair game, but it seems more likely than not. So in this change, that is now allowed.
  • Awaiting a TaskCompletionSource<T>.Task property on a TCS that was created in that same method. Test methods often do this. Awaiting a TCS.Task property is never guaranteed to be safe as far as analyzers can tell (though the 3rd threading rule can make it safe), and when common patterns appear that are typically safe, we should allow it.

As a part of this change, I updated a couple utility functions, including in at least one way that wasn't directly relevant to these changes but seemed like a good change considering the nature of the analyzer bugs that keep coming in (e.g. recognizing LocalFunctionDeclarationSyntax in more places).
I also made the diagnostic's location focus on the awaited object itself rather than including the .ConfigurueAwait that might follow it.

@AArnott AArnott added this to the v17.9 milestone Oct 25, 2023
When integrating into the VS repo, recent enhancements to VSTHRD003 made insertion of the new analyzers problematic. In particular, two patterns were quite common and arguably should be allowed:

- Awaiting on the `JoinableTask.Task` property on the result of a `JoinableTaskFactory.RunAsync` method call. In general, if a method returns an object, awaiting one of that object's properties *might not* be fair game, but it seems more likely than not. So in this change, that is now allowed.
- Awaiting a `TaskCompletionSource<T>.Task` property on a TCS that was created in that same method. Test methods often do this. Awaiting a TCS.Task property is never guaranteed to be safe as far as analyzers can tell (though the 3rd threading rule can make it safe), and when common patterns appear that are _typically_ safe, we should allow it.
@AArnott
AArnott enabled auto-merge October 25, 2023 20:31
@AArnott
AArnott merged commit cf86d12 into microsoft:main Oct 25, 2023
@AArnott
AArnott deleted the analyzerFixes branch October 25, 2023 20:50
@sandyarmstrong

Copy link
Copy Markdown
Member

Awaiting a TaskCompletionSource.Task property on a TCS that was created in that same method. Test methods often do this. Awaiting a TCS.Task property is never guaranteed to be safe as far as analyzers can tell (though the 3rd threading rule can make it safe), and when common patterns appear that are typically safe, we should allow it.

@AArnott I still see this scenario triggering VSTHRD003 with 17.9.27, even with code as simple as return new TaskCompletionSource<object?>().Task.

@AArnott

AArnott commented Dec 12, 2023

Copy link
Copy Markdown
Member Author

@sandyarmstrong If you can provide a specific repro that we can add as a test case, I'll be happy to take a look at it. The specific example you just gave would not make a compelling test case because it would actually be a bug, since the returned Task would be uncompletable. I'd like a test case that demonstrates correct code that is being mis-flagged.

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.

3 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.