diff --git a/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerDriver.cs b/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerDriver.cs index 89060a9af3a71326e088c1f1a15d5b37c9abd23c..8f2e44f97b13f080957237984bd05ac3d35d31b3 100644 --- a/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerDriver.cs +++ b/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerDriver.cs @@ -1853,6 +1853,11 @@ private static void ComputeDeclarationsInNode(SemanticModel semanticModel, ISymb // Mark completion if we successfully executed all actions and only if we are analyzing a span containing the entire syntax node. if (success && analysisStateOpt != null && !declarationAnalysisData.IsPartialAnalysis) { + // Ensure that we do not mark declaration complete/clear state if cancellation was requested. + // Other thread(s) might still be executing analysis, and clearing state could lead to corrupt execution + // or unknown exceptions. + cancellationToken.ThrowIfCancellationRequested(); + foreach (var analyzer in analysisScope.Analyzers) { analysisStateOpt.MarkDeclarationComplete(symbol, declarationIndex, analyzer);