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] Unawaited / returned tasks created with a disposable in using block for that disposable #78394

Open
stephentoub opened this issue Nov 15, 2022 · 2 comments
Labels
api-approved API was approved in API review, it can be implemented area-System.Threading.Tasks code-analyzer Marks an issue that suggests a Roslyn analyzer help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@stephentoub
Copy link
Member

stephentoub commented Nov 15, 2022

Consider code like the following:

public Task MyMethodAsync()
{
    using (SomeIDisposable disposable = new SomeIDisposable())
    {
        ...
        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.

@stephentoub stephentoub added the code-analyzer Marks an issue that suggests a Roslyn analyzer label Nov 15, 2022
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Nov 15, 2022
@ghost
Copy link

ghost commented Nov 15, 2022

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

Issue Details

Consider 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.

Author: stephentoub
Assignees: -
Labels:

area-System.Threading.Tasks, code-analyzer

Milestone: -

@buyaa-n buyaa-n added this to the 8.0.0 milestone Nov 16, 2022
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Nov 16, 2022
@stephentoub stephentoub added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Nov 21, 2022
@stephentoub stephentoub added api-ready-for-review API is ready for review, it is NOT ready for implementation and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation labels Jan 20, 2023
@bartonjs
Copy link
Member

bartonjs commented 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.

    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
Severity: Warning

@bartonjs bartonjs added api-approved API was approved in API review, it can be implemented and removed api-ready-for-review API is ready for review, it is NOT ready for implementation labels Mar 7, 2023
@tannergooding tannergooding modified the milestones: 8.0.0, Future Jul 24, 2023
@buyaa-n buyaa-n added the help wanted [up-for-grabs] Good issue for external contributors label Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-approved API was approved in API review, it can be implemented area-System.Threading.Tasks code-analyzer Marks an issue that suggests a Roslyn analyzer help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

No branches or pull requests

4 participants