You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Analyzer] Unawaited / returned tasks created with a disposable in using block for that disposable #78394
publicTaskMyMethodAsync(){using(SomeIDisposabledisposable=newSomeIDisposable()){
...
return SomethingAsync(disposable);// or fire-and-forget}}
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.
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)
A possible false positive that hasn't been considered would come from the way awaited tasks are considered.
Technically this would fire, even though the task is awaited before the arguments are disposed:
Activity
bartonjs commentedon Mar 7, 2023
Video
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.
)
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
Severity: Warning
steveberdy commentedon Dec 6, 2024
I'd like to be assigned this for .NET 10
stephentoub commentedon Dec 6, 2024
Great, thanks!
steveberdy commentedon May 8, 2025
A possible false positive that hasn't been considered would come from the way awaited tasks are considered.
Technically this would fire, even though the task is awaited before the arguments are disposed:
akoeplinger commentedon May 8, 2025
@steveberdy yeah I think that's one of the cases I hit in dotnet/dotnet@95674bb
steveberdy commentedon May 8, 2025
@akoeplinger yes, it only handles awaits, .Result, and .Wait() currently.
.GetAwaiter().GetResult() should be easy to cover as well.