Skip to content

Add analyzer to suggest using cooperative cancellation for timeout #5786

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

Merged
merged 13 commits into from
Jun 18, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 16, 2025

This PR introduces a new analyzer MSTEST0045 that warns users when they use [Timeout] attribute without explicitly setting CooperativeCancellation = true. This prepares users for a future breaking change where cooperative cancellation will become the default behavior.

Problem

Currently, the TimeoutAttribute has a CooperativeCancellation property that defaults to false. In a future version (v5.0), cooperative cancellation will become the default behavior. To help users prepare for this breaking change, we need an analyzer that encourages them to explicitly opt into the recommended behavior.

Solution

The new analyzer UseCooperativeCancellationForTimeoutAnalyzer detects:

  • [Timeout] attributes on test methods without CooperativeCancellation specified
  • [Timeout] attributes with CooperativeCancellation = false explicitly set

And reports an Info-level diagnostic suggesting to use CooperativeCancellation = true.

Examples

❌ Will trigger diagnostic:

[TestMethod]
[Timeout(5000)] // Missing CooperativeCancellation
public void MyTest() { }

[TestMethod]  
[Timeout(5000, CooperativeCancellation = false)] // Explicitly false
public void MyTest() { }

✅ No diagnostic:

[TestMethod]
[Timeout(5000, CooperativeCancellation = true)] // Explicitly true - recommended
public void MyTest() { }

Implementation Details

  • Analyzer ID: MSTEST0045
  • Category: Usage
  • Severity: Info
  • Scope: Only analyzes [Timeout] attributes on test methods ([TestMethod], [DataTestMethod], etc.)

Files Added/Modified

  • UseCooperativeCancellationForTimeoutAnalyzer.cs - Main analyzer implementation
  • UseCooperativeCancellationForTimeoutAnalyzerTests.cs - Comprehensive unit tests
  • ✅ Updated WellKnownTypeNames.cs to include TimeoutAttribute
  • ✅ Updated DiagnosticIds.cs with MSTEST0045
  • ✅ Updated resource files with localized diagnostic messages
  • ✅ Updated public API and analyzer release documentation

Test Coverage

The implementation includes comprehensive tests covering:

  • Timeout without cooperative cancellation → diagnostic
  • Timeout with CooperativeCancellation = true → no diagnostic
  • Timeout with CooperativeCancellation = false → diagnostic
  • Timeout on non-test methods → no diagnostic
  • Various test method types (TestMethod, DataTestMethod)
  • TestTimeout enum usage scenarios

Fixes #5761.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • cflvsblobprodcus383.vsblob.vsassets.io
    • Triggering command: /home/REDACTED/work/testfx/testfx/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/testfx/testfx/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/testfx/testfx/artifacts/toolset/10.0.0-beta.25313.2.txt (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 3 commits June 16, 2025 14:19
Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Add analyzer to suggest using cooperative cancellation for timeout Add analyzer to suggest using cooperative cancellation for timeout Jun 16, 2025
@Copilot Copilot AI requested a review from Youssef1313 June 16, 2025 14:23
Copilot finished work on behalf of Youssef1313 June 16, 2025 14:23
Copy link
Member

@Evangelink Evangelink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also add the codefix related to this analyzer please.

Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 17, 2025

Let's also add the codefix related to this analyzer please.

Added codefix for the UseCooperativeCancellationForTimeout analyzer. The codefix automatically transforms [Timeout] attributes to include CooperativeCancellation = true. Includes comprehensive test coverage for all scenarios. Commit 6615e56.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • cflvsblobprodcus383.vsblob.vsassets.io
    • Triggering command: /home/REDACTED/work/testfx/testfx/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/testfx/testfx/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/testfx/testfx/artifacts/toolset/10.0.0-beta.25313.2.txt (dns block)
    • Triggering command: ./.dotnet/dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj --verbosity quiet (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of Evangelink June 17, 2025 07:43
Copy link
Member

@Evangelink Evangelink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The timeout attribute can be also specified on all fixture methods (e.g. ClassInitialize) so please remove the check for TestMethodAttribute, checking only TimeoutAttribute is enough. There is another rule to ensure the TimeoutAttribute is used at the right location.

Please update codefixer and tests!

…d with Timeout attribute

Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 17, 2025

The timeout attribute can be also specified on all fixture methods (e.g. ClassInitialize) so please remove the check for TestMethodAttribute, checking only TimeoutAttribute is enough. There is a...

Removed the check for TestMethodAttribute - the analyzer now triggers on any method with [Timeout] attribute, including all fixture methods (ClassInitialize, AssemblyInitialize, etc.). Updated tests to cover all fixture method scenarios and updated the codefix accordingly. Commit 8fc728a.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • cflvsblobprodcus383.vsblob.vsassets.io
    • Triggering command: /home/REDACTED/work/testfx/testfx/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/testfx/testfx/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/testfx/testfx/artifacts/toolset/10.0.0-beta.25313.2.txt (dns block)
    • Triggering command: ./.dotnet/dotnet build src/Analyzers/MSTest.Analyzers/MSTest.Analyzers.csproj --verbosity quiet (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of Evangelink June 17, 2025 07:54
Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Copilot finished work on behalf of Evangelink June 17, 2025 07:59
@Evangelink Evangelink marked this pull request as ready for review June 17, 2025 19:12
@Evangelink Evangelink enabled auto-merge (squash) June 17, 2025 19:17
Evangelink
Evangelink previously approved these changes Jun 17, 2025
Evangelink
Evangelink previously approved these changes Jun 18, 2025
@Evangelink Evangelink merged commit f036f44 into main Jun 18, 2025
9 checks passed
@Evangelink Evangelink deleted the copilot/fix-5761 branch June 18, 2025 12:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add analyzer to suggest using cooperative cancellation for timeout
3 participants