Repository navigation
Add CA2027: Detect non-cancelable Task.Delay in Task.WhenAny - #51452
Conversation
stephentoub
left a comment
There was a problem hiding this comment.
@copilot, see the build failures:
One or more auto-generated documentation files were either edited manually, or not updated. Please revert changes made to the following files (if manually edited) and run `msbuild /t:pack` at the root of the repo to automatically update them:
D:\a\_work\1\s\src\Microsoft.CodeAnalysis.NetAnalyzers\src\Microsoft.CodeAnalysis.NetAnalyzers.md
D:\a\_work\1\s\src\Microsoft.CodeAnalysis.NetAnalyzers\src\Microsoft.CodeAnalysis.NetAnalyzers.sarif
Fixed in c5ddc49. Ran |
There was a problem hiding this comment.
Pull Request Overview
This PR implements analyzer CA2027 to detect non-cancelable Task.Delay calls used with Task.WhenAny, which can waste resources by leaving timers running after the operation completes. The analyzer provides platform-agnostic guidance recommending Task.WaitAsync when available or the CancellationToken pattern otherwise.
Key changes:
- Created analyzer-only rule (no code fixer) that flags
Task.Delaycalls without cancellation tokens inTask.WhenAnyoperations - Skips single-task scenarios to avoid false positives
- Supports both implicit params arrays and explicit array/collection expressions
Reviewed Changes
Copilot reviewed 21 out of 21 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| DoNotUseNonCancelableTaskDelayWithWhenAny.cs | Core analyzer implementation detecting non-cancelable Task.Delay in Task.WhenAny |
| DoNotUseNonCancelableTaskDelayWithWhenAnyTests.cs | Comprehensive test coverage for C# and VB scenarios |
| MicrosoftNetCoreAnalyzersResources.resx | English resource strings for diagnostic messages |
| MicrosoftNetCoreAnalyzersResources.*.xlf | Localization files marking new strings for translation |
| DiagnosticCategoryAndIdRanges.txt | Updates reliability range to include CA2027 |
| AnalyzerReleases.Unshipped.md | Documents CA2027 as new reliability rule |
| Microsoft.CodeAnalysis.NetAnalyzers.sarif | SARIF metadata for CA2027 |
| Microsoft.CodeAnalysis.NetAnalyzers.md | Auto-generated documentation for CA2027 |
| RulesMissingDocumentation.md | Reverted unrelated changes |
983c118 to
6415242
Compare
Head branch was pushed to by a user without write access
|
@stephentoub just to make sure you saw the comment #51452 (comment). |
4a570d6 to
dc7bba0
Compare
|
/ba-g dead letter |
Latest Changes
RulesMissingDocumentation.md restoration:
The analyzer is complete and all tests pass.
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.