-
Couldn't load subscription status.
- Fork 1.2k
Add CA2027: Detect non-cancelable Task.Delay in Task.WhenAny #51452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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
...odeAnalysis.NetAnalyzers/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
...alyzers/Microsoft.NetCore.Analyzers/Tasks/DoNotUseNonCancelableTaskDelayWithWhenAny.Fixer.cs
Outdated
Show resolved
Hide resolved
....NetAnalyzers/Microsoft.NetCore.Analyzers/Tasks/DoNotUseNonCancelableTaskDelayWithWhenAny.cs
Outdated
Show resolved
Hide resolved
....NetAnalyzers/Microsoft.NetCore.Analyzers/Tasks/DoNotUseNonCancelableTaskDelayWithWhenAny.cs
Outdated
Show resolved
Hide resolved
....NetAnalyzers/Microsoft.NetCore.Analyzers/Tasks/DoNotUseNonCancelableTaskDelayWithWhenAny.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.CodeAnalysis.NetAnalyzers/src/RulesMissingDocumentation.md
Show resolved
Hide resolved
...nitTests/Microsoft.NetCore.Analyzers/Tasks/DoNotUseNonCancelableTaskDelayWithWhenAnyTests.cs
Outdated
Show resolved
Hide resolved
....NetAnalyzers/Microsoft.NetCore.Analyzers/Tasks/DoNotUseNonCancelableTaskDelayWithWhenAny.cs
Outdated
Show resolved
Hide resolved
Fixed in c5ddc49. Ran |
...odeAnalysis.NetAnalyzers/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
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 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 |
....NetAnalyzers/Microsoft.NetCore.Analyzers/Tasks/DoNotUseNonCancelableTaskDelayWithWhenAny.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.CodeAnalysis.NetAnalyzers/src/RulesMissingDocumentation.md
Show resolved
Hide resolved
...nitTests/Microsoft.NetCore.Analyzers/Tasks/DoNotUseNonCancelableTaskDelayWithWhenAnyTests.cs
Outdated
Show resolved
Hide resolved
983c118 to
6415242
Compare
Co-authored-by: tarekgh <[email protected]>
Head branch was pushed to by a user without write access
|
@stephentoub just to make sure you saw the comment #51452 (comment). |
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.