Add AZC0020 analyzer to enforce CancellationToken propagation via RequestContext#56230
Add AZC0020 analyzer to enforce CancellationToken propagation via RequestContext#56230
Conversation
…text - Added new analyzer to detect when CancellationToken is not propagated to RequestContext - Added diagnostic descriptor AZC0020 with warning severity - Implemented RequestContextCancellationTokenAnalyzer with support for: - Method invocations with RequestContext parameters - Object initializer syntax for RequestContext - Lambda expressions and anonymous functions - Proper handling of pre-existing RequestContext objects - Added comprehensive test suite with 11 test cases covering various scenarios - All tests passing successfully Co-authored-by: christothes <1279263+christothes@users.noreply.github.com>
- Created AZC0020.md with detailed description and examples - Updated list-of-diagnostics.md to include AZC0020 - Updated README.md to reference the new analyzer rule - All tests passing in Debug and Release configurations Co-authored-by: christothes <1279263+christothes@users.noreply.github.com>
sdk/tools/Azure.SdkAnalyzers/tests/Azure.SdkAnalyzers.Tests/AZC0020Tests.cs
Show resolved
Hide resolved
- Updated analyzer to recognize extension method calls on CancellationToken - Added detection for ToRequestContext() and ToRequestOptions() methods - Added test case AZC0020NotProducedWhenUsingToRequestContextExtension - All 12 AZC0020 tests passing Co-authored-by: christothes <1279263+christothes@users.noreply.github.com>
- Replaced specific extension method name checking with general approach - Added ContainsCancellationTokenReference method that recursively checks operation tree - Now detects any method/expression that uses the CancellationToken parameter - Works with extension methods, helper methods, and any custom patterns - Added test case for custom helper method pattern - All 13 AZC0020 tests passing Co-authored-by: christothes <1279263+christothes@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR adds a new Roslyn analyzer (AZC0020) to enforce proper CancellationToken propagation to RequestContext parameters in Azure SDK protocol API calls. The analyzer addresses a gap in existing analyzers (like CA2016) which only detect missing token propagation when the callee explicitly accepts a CancellationToken parameter, but not when the cancellation is conveyed through RequestContext objects.
Changes:
- Implements a new diagnostic analyzer (AZC0020) using operation-based analysis with recursive operation tree traversal to detect when methods with CancellationToken parameters call Azure SDK APIs without propagating the token to RequestContext
- Adds comprehensive test coverage with 13 test cases covering various scenarios including extension methods, helper methods, lambda expressions, and edge cases
- Provides complete documentation including diagnostic descriptor, rule documentation (AZC0020.md), and updates to README and list-of-diagnostics
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| RequestContextCancellationTokenAnalyzer.cs | Core analyzer implementation using IInvocationOperation with recursive operation tree traversal to detect missing CancellationToken propagation; handles object initializers, extension methods, helper methods, and conservatively skips pre-existing RequestContext references |
| Descriptors.cs | Adds AZC0020 diagnostic descriptor with warning severity and clear messaging about CancellationToken propagation requirement |
| AZC0020Tests.cs | Comprehensive test suite with 13 test cases covering positive/negative scenarios, edge cases (lambdas, extension methods, helper methods), and boundary conditions |
| AZC0020.md | Detailed documentation explaining the rule, rationale, fix guidance, and examples |
| list-of-diagnostics.md | Adds AZC0020 to the diagnostics list with description, violation examples, fix examples, and notes about analyzer behavior |
| README.md | Updates the implemented rules list to include AZC0020 |
| var containingNamespace = method.ContainingNamespace; | ||
| while (containingNamespace != null && !containingNamespace.IsGlobalNamespace) | ||
| { | ||
| if (containingNamespace.Name == "Azure") |
There was a problem hiding this comment.
Should we also check for Microsoft.Azure since that's our bridge package, extensions package, and some other conventions?
I wouldn't expect that we'd encounter any RequestContext use in the T1 packages, so checking them shouldn't produce violations, right?
Description
Azure SDK protocol APIs accept
RequestContextinstead of directCancellationTokenparameters. Existing analyzers (CA2016) only detect missing token propagation when the callee explicitly acceptsCancellationToken, allowing cancellation to be silently dropped inRequestContextscenarios.Changes
New Analyzer (AZC0020)
CancellationTokenparameters call Azure SDK APIs without propagating the token toRequestContextAzurenamespace) to avoid false positivesRequestContextobjects from parameters/locals where token state is indeterminateImplementation
RequestContextCancellationTokenAnalyzer.cs: Operation-based analyzer usingIInvocationOperationwith recursive operation tree traversalDescriptors.cs: AZC0020 diagnostic descriptor (warning severity)AZC0020Tests.cs: 13 test cases covering positive/negative scenarios, including extension methods and custom helper patternsExample
Violation detected:
Fixed (object initializer):
Fixed (extension method):
Fixed (helper method):
This checklist is used to make sure that common guidelines for a pull request are followed.
General Guidelines
Testing Guidelines
SDK Generation Guidelines
*.csprojandAssemblyInfo.csfiles have been updated with the new version of the SDK.Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.