Skip to content

Recognize safely completed Task results in VSTHRD103 - #1647

Merged
Andrew Arnott (AArnott) merged 1 commit into
mainfrom
aarnott-fix-vsthrd103-flow-suppressions
Aug 22, 2026
Merged

Andrew Arnott (AArnott) merged 1 commit into
mainfrom
aarnott-fix-vsthrd103-flow-suppressions

Conversation

@AArnott

Copy link
Copy Markdown
Member

Summary

  • suppress VSTHRD103 for Task.Result when the same stable task is proven complete by IsCompletedSuccessfully
  • suppress VSTHRD103 for Result on the actual antecedent parameter of a BCL Task.ContinueWith callback
  • share the conservative completion proof with VSTHRD104 and preserve diagnostics for reassignment, ref aliasing, captured storage, state delegates, loop backedges, local-function mutation, and deferred query execution

Fixes #1125
Fixes #680

Issue #1480

This does not globally suppress EF Core DbSet.AddRange. EF Core documents that AddRangeAsync is required for special asynchronous value generators, so the analyzer cannot determine from the call site whether the synchronous operation is appropriate. A built-in suppression would hide valid VSTHRD103 diagnostics. Projects that do not use such generators can use the existing vs-threading.SyncMethodsToExcludeFromVSTHRD103.txt configuration with:

[Microsoft.EntityFrameworkCore.DbSet`1]::AddRange

References #1480.

Validation

  • Release build
  • full Microsoft.VisualStudio.Threading.Analyzers.Tests suite across net472, net8.0, and net8.0-windows

Share conservative Task completion analysis between VSTHRD103 and VSTHRD104. Suppress Result diagnostics for stable IsCompletedSuccessfully guards and the actual antecedent parameter of BCL Task.ContinueWith callbacks, while retaining diagnostics across writes, aliases, captured storage, and deferred execution boundaries.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings August 21, 2026 16:38

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR improves the C# analyzers for VSTHRD103 and VSTHRD104 by recognizing additional patterns where Task<TResult>.Result access is guaranteed not to block, specifically when the task is conservatively proven completed (e.g., guarded by IsCompletedSuccessfully) and for the antecedent parameter inside BCL Task.ContinueWith callbacks.

Changes:

  • Introduces a shared TaskCompletionAnalysis helper to conservatively prove completion at Result access sites.
  • Updates VSTHRD103 and VSTHRD104 analyzers to suppress diagnostics for proven-safe Task.Result access (including ContinueWith antecedent usage).
  • Adds targeted regression tests covering guarded completion, reassignment/ref aliasing, loop backedges, local function mutation, and deferred query scenarios.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Microsoft.VisualStudio.Threading.Analyzers.CSharp/TaskCompletionAnalysis.cs Adds shared conservative completion-proof logic for safe Task.Result access.
src/Microsoft.VisualStudio.Threading.Analyzers.CSharp/VSTHRD103UseAsyncOptionAnalyzer.cs Uses TaskCompletionAnalysis to suppress VSTHRD103 when Result is proven non-blocking.
src/Microsoft.VisualStudio.Threading.Analyzers.CSharp/VSTHRD104OfferAsyncOptionAnalyzer.cs Uses TaskCompletionAnalysis to suppress VSTHRD104 when Result is proven non-blocking.
test/Microsoft.VisualStudio.Threading.Analyzers.Tests/VSTHRD103UseAsyncOptionAnalyzerTests.cs Adds coverage for the new suppression/proof behavior and conservative non-suppression cases.
test/Microsoft.VisualStudio.Threading.Analyzers.Tests/VSTHRD104OfferAsyncOptionAnalyzerTests.cs Adds regression tests ensuring conservative behavior is preserved for tricky mutation/ref/loop cases.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@AArnott
Andrew Arnott (AArnott) merged commit 7601e49 into main Aug 22, 2026
10 checks passed
@AArnott
Andrew Arnott (AArnott) deleted the aarnott-fix-vsthrd103-flow-suppressions branch August 22, 2026 02:57
This was referenced Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants