-
Notifications
You must be signed in to change notification settings - Fork 5k
[Analyzer] Unawaited / returned tasks created with a disposable in using block for that disposable #78394
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
Comments
Tagging subscribers to this area: @dotnet/area-system-threading-tasks Issue DetailsConsider code like the following: public Task MyMethodAsync()
{
using (SomeIDisposable disposable = new SomeIDisposable())
{
...
return SomethingAsync(disposable);
}
} There's a high likelihood that this Task-returning SomethingAsync will be using the SomeIDisposable instance long after it's been disposed. We should consider an analyzer that flags such situations.
|
While it can't detect cases where the IDisposable got wrapped into another parameter, it does seem like it provides value for the ones it detected. It would also be nice if it could detect the dispose via a manual try-finally or direct imperative code (e.g. SomeIDisposable disposable = new SomeIDisposable();
Task t = SomethingAsync(disposable);
disposable.Dispose();
return t; ) And it might be further nice if we could generalize it to rent/return patterns like ArrayPool, but that's perhaps getting a bit fantastical (and each would require a custom flow analyzer, or putting some sort of attribute on IDisposable.Dispose() (and all aliasing Close() methods) and ArrayPool.Return) Category: Reliability |
I'd like to be assigned this for .NET 10 |
Great, thanks! |
A possible false positive that hasn't been considered would come from the way awaited tasks are considered. var x = new DisposableThing();
var task = DoSomethingAsync(x);
var task2 = ...;
Task.WaitAll(task, task2);
// or
await Task.WhenAll(task, task2);
x.Dispose(); |
@steveberdy yeah I think that's one of the cases I hit in dotnet/dotnet@95674bb |
@akoeplinger yes, it only handles awaits, .Result, and .Wait() currently. |
Uh oh!
There was an error while loading. Please reload this page.
Consider code like the following:
There's a high likelihood that this Task-returning SomethingAsync will be using the SomeIDisposable instance long after it's been disposed. We should consider an analyzer that flags such situations.
The text was updated successfully, but these errors were encountered: