Skip to content

[Analyzer] Unawaited / returned tasks created with a disposable in using block for that disposable #78394

Closed
dotnet/roslyn-analyzers
#7549

Description

@stephentoub

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.

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Nov 15, 2022
  2. added this to the 8.0.0 milestone on Nov 16, 2022
  3. ghost removed
    untriagedNew issue has not been triaged by the area owner
    on Nov 16, 2022
  4. added
    api-suggestionEarly API idea and discussion, it is NOT ready for implementation
    on Nov 21, 2022
  5. added
    api-ready-for-reviewAPI is ready for review, it is NOT ready for implementation
    and removed
    api-suggestionEarly API idea and discussion, it is NOT ready for implementation
    on Jan 20, 2023
  6. bartonjs commented on Mar 7, 2023

    @bartonjs
    Member

    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

  7. added
    api-approvedAPI was approved in API review, it can be implemented
    and removed
    api-ready-for-reviewAPI is ready for review, it is NOT ready for implementation
    on Mar 7, 2023
  8. modified the milestones: 8.0.0, Future on Jul 24, 2023
  9. added
    help wanted[up-for-grabs] Good issue for external contributors
    on Apr 2, 2024
  10. steveberdy commented on Dec 6, 2024

    @steveberdy
    Contributor

    I'd like to be assigned this for .NET 10

  11. stephentoub commented on Dec 6, 2024

    @stephentoub
    MemberAuthor

    I'd like to be assigned this for .NET 10

    Great, thanks!

  12. steveberdy commented on May 8, 2025

    @steveberdy
    Contributor

    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:

    var x = new DisposableThing();
    var task = DoSomethingAsync(x);
    var task2 = ...;
    
    Task.WaitAll(task, task2);
    // or
    await Task.WhenAll(task, task2);
    
    x.Dispose();
  13. akoeplinger commented on May 8, 2025

    @akoeplinger
    Member

    @steveberdy yeah I think that's one of the cases I hit in dotnet/dotnet@95674bb

  14. steveberdy commented on May 8, 2025

    @steveberdy
    Contributor

    @akoeplinger yes, it only handles awaits, .Result, and .Wait() currently.
    .GetAwaiter().GetResult() should be easy to cover as well.

  15. locked and limited conversation to collaborators on Jun 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.Threading.Taskscode-analyzerMarks an issue that suggests a Roslyn analyzerhelp wanted[up-for-grabs] Good issue for external contributors

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions