Skip to content
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

[API Proposal]: Code analyse rule that require passing of cancellation token #82889

Closed
jarlef opened this issue Mar 2, 2023 · 3 comments
Closed
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Threading.Tasks

Comments

@jarlef
Copy link

jarlef commented Mar 2, 2023

Background and motivation

The rule CA2016 checks that any in scope cancellation token must be forwarded, but if the current method does not have a cancellation token there rule does result in a violation

public async Task Foo(CancellationToken token) 
{
  await SomeMethodThatAcceptsCancellationToken(); // CA2016 violation
}

public async Task Bar() 
{
  await SomeMethodThatAcceptsCancellationToken(); // no violation
}

This can be hard to identify that the cancellation token is not forwarded down the stack as it should have been since the second method does not accept a token as a input parameter. There should be a even more strict rule that requires the forwarding of the cancellation token or to use CancellationToken.None if the cancellation token is not need / available.

public async Task Bar() 
{
  await SomeMethodThatAcceptsCancellationToken(CancellationToken.None);
}

This will make it easier to maintain the code and also perform code reviews on the use of cancellation tokens if the code base.

API Proposal

public async Task Bar() 
{
  await SomeMethodThatAcceptsCancellationToken(); // CA20XX violation
}

API Usage

public async Task Bar() 
{
  await SomeMethodThatAcceptsCancellationToken(); // CA20XX violation
}

Alternative Designs

No response

Risks

No response

@jarlef jarlef added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Mar 2, 2023
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Mar 2, 2023
@ghost
Copy link

ghost commented Mar 2, 2023

Tagging subscribers to this area: @dotnet/area-system-threading-tasks
See info in area-owners.md if you want to be subscribed.

Issue Details

Background and motivation

The rule CA2016 checks that any in scope cancellation token must be forwarded, but if the current method does not have a cancellation token there rule does result in a violation

public async Task Foo(CancellationToken token) 
{
  await SomeMethodThatAcceptsCancellationToken(); // violation
}

public async Task Bar() 
{
  await SomeMethodThatAcceptsCancellationToken(); // no violation
}

This can be hard to identify that the cancellation token is not forwarded down the stack as it should have been since the second method does not accept a token as a input parameter. There should be a even more strict rule that requires the forwarding of the cancellation token or to use CancellationToken.None if the cancellation token is not need / available.

public async Task Bar() 
{
  await SomeMethodThatAcceptsCancellationToken(CancellationToken.None);
}

This will make it easier to maintain the code and also perform code reviews on the use of cancellation tokens if the code base.

API Proposal

public async Task Bar() 
{
  await SomeMethodThatAcceptsCancellationToken(); // CA20XX violation
}

API Usage

public async Task Bar() 
{
  await SomeMethodThatAcceptsCancellationToken(); // CA20XX violation
}

Alternative Designs

No response

Risks

No response

Author: jarlef
Assignees: -
Labels:

api-suggestion, area-System.Threading.Tasks

Milestone: -

@stephentoub
Copy link
Member

Thanks. This is largely a duplicate of #78397, which proposes a rule that async methods should accept a token. I'm less inclined to have a rule about calls that aren't passed a token and don't have one to pass, as that's really just a stylistic choice as to whether to use the implicit default token or the explicit default token with None.

@stephentoub
Copy link
Member

duplicate of #78397

@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Jun 7, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Jul 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Threading.Tasks
Projects
None yet
Development

No branches or pull requests

2 participants