roslyn: AD0001 should not be reported for expected cancellation

Version Used: 15.9 Preview 2

Steps to Reproduce:

  1. Write a SyntaxNode analyzer
  2. In the analyzer, call context.CancellationToken.ThrowIfCancellationRequested()

Expected Behavior:

In cancellation scenarios, the IDE silently stops analysis and continues execution.

Actual Behavior:

The IDE reports AD0001 for the analyzer throwing an exception. Here is an example:

Severity	Code	Description	Project	File	Line	Suppression State	Detail Description
Error	AD0001	Analyzer 'Microsoft.VisualStudio.Threading.Analyzers.VSTHRD001UseSwitchToMainThreadAsyncAnalyzer' threw an exception of type 'System.Exception' with message 'Analyzer failure while processing syntax at D:\dev\roslyn\src\Compilers\CSharp\Portable\Compiler\MethodCompiler.cs(924,21): System.OperationCanceledException The operation was canceled.. Syntax: Debug.Assert(methodSymbol.IsImplicitlyDeclared)'.	Microsoft.CodeAnalysis.CSharp		1	Active	Analyzer 'Microsoft.VisualStudio.Threading.Analyzers.VSTHRD001UseSwitchToMainThreadAsyncAnalyzer' threw the following exception:
'Exception occurred with following context:
Compilation: Microsoft.CodeAnalysis.CSharp
SyntaxTree: D:\dev\roslyn\src\Compilers\CSharp\Portable\Compiler\MethodCompiler.cs
SyntaxNode: Debug.Assert(methodSymbol.IsImplicitlyDeclared ... [InvocationExpressionSyntax]@[45231..45278) (923,20)-(923,67)

System.Exception: Analyzer failure while processing syntax at D:\dev\roslyn\src\Compilers\CSharp\Portable\Compiler\MethodCompiler.cs(924,21): System.OperationCanceledException The operation was canceled.. Syntax: Debug.Assert(methodSymbol.IsImplicitlyDeclared) ---> System.OperationCanceledException: The operation was canceled.
   at System.Threading.CancellationToken.ThrowOperationCanceledException()
   at Microsoft.VisualStudio.Threading.Analyzers.VSTHRD001UseSwitchToMainThreadAsyncAnalyzer.AnalyzeInvocation(SyntaxNodeAnalysisContext context)
   at Microsoft.VisualStudio.Threading.Analyzers.Utils.<>c__DisplayClass0_0.<DebuggableWrapper>b__0(SyntaxNodeAnalysisContext ctxt)
   --- End of inner exception stack trace ---
   at Microsoft.VisualStudio.Threading.Analyzers.Utils.<>c__DisplayClass0_0.<DebuggableWrapper>b__0(SyntaxNodeAnalysisContext ctxt)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__44`1.<ExecuteSyntaxNodeAction>b__44_0(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)
-----
System.OperationCanceledException: The operation was canceled.
   at System.Threading.CancellationToken.ThrowOperationCanceledException()
   at Microsoft.VisualStudio.Threading.Analyzers.VSTHRD001UseSwitchToMainThreadAsyncAnalyzer.AnalyzeInvocation(SyntaxNodeAnalysisContext context)
   at Microsoft.VisualStudio.Threading.Analyzers.Utils.<>c__DisplayClass0_0.<DebuggableWrapper>b__0(SyntaxNodeAnalysisContext ctxt)
-----
'.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 23 (21 by maintainers)

Commits related to this issue

Most upvoted comments

This is because we wrap OperationCanceledException along with all other exceptions thrown from analyzers in an Exception that gives much more context about where the error occurred so that such error reports are more actionable. We didn’t mean to or need to wrap OCE in particular, but we hadn’t had a special case to let that slip through. We fixed this in https://github.com/Microsoft/vs-threading/commit/061223411ac5b545fac24ff7c133bde57334033e and https://github.com/Microsoft/vs-threading/commit/f88fbddfdf4090d76535ba38e9594fe97adb2c22, which both shipped with v15.8.168 of our analyzers. Are you running something older than that?