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

Analyzer to suggest using ReentrantSemaphore instead of task chaining #306

Open
AArnott opened this issue Jul 3, 2018 · 4 comments
Open

Comments

@AArnott
Copy link
Member

AArnott commented Jul 3, 2018

Is your feature request related to a problem? Please describe.

People regularly use task chaining to accomplish a serialized, in-order execution of arbitrary operations. For example:

class Foo {
  Task lastTask = Task.CompletedTask;

  Task AddOperation(Action action) {
     return lastTask = lastTask.ContinueWith(_ => action);
  }
}

This trivial example doesn't support Func<Task> functions, and doing so properly is tricky. Avoiding deadlocks in this pattern is particularly tricky as it must be made compliant with the 3rd threading rule.

Describe the solution you'd like

An analyzer that detects when continuations of a Task field is assigned back to the original field and reports a diagnostic (warning or info) suggesting that ReentrantSemaphore be used instead.

@sharwell
Copy link
Member

The thing that makes me the most uncomfortable here is the semantics of ReentrantSemaphore. I need to revisit that type to better understand it.

@AArnott
Copy link
Member Author

AArnott commented Aug 19, 2018

I'm interested in your feedback when you are ready to share it.

@sharwell
Copy link
Member

@AArnott task chaining is a frequent item I hit, and improving it would be a high value win IMO. You may be completely right about this analyzer already but I can be a tough sell so I really want to convince myself that the solution is good all around. Whatever path we use, I expect if we ever want to change it, it will be even harder than switching away from task chaining.

@AArnott
Copy link
Member Author

AArnott commented Aug 20, 2018

There may be more than one use case for task chaining. The one I'm interested in (and that hopefully the analyzer can be taught to recognize) is when it's used to limit concurrent execution to 1 without affinitizing to a particular thread.

AArnott added a commit that referenced this issue Jan 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants