Skip to content

[API Proposal]: Implement IAsyncDisposable on TextReader #88244

Open

Description

@Neme12

Background and motivation

By default, when disposing a StreamReader or StreamWriter, the underlying Stream itself is disposed. Since Stream supports async disposal via IAsyncDisposable, both StreamReader and StreamWriter should implement IAsyncDisposable as well to support the case when the underlying stream supports it. Currently, only StreamWriter implements IAsyncDisposable while StreamReader doesn't, so when using StreamReader, it's only possible to dispose the underlying stream synchronously.

API Proposal

namespace System.IO;

// new
public abstract partial class TextReader : MarshalByRefObject, IDisposable, IAsyncDisposable
{
    public virtual ValueTask DisposeAsync();
}

// existing
public abstract partial class TextWriter : MarshalByRefObject, IDisposable, IAsyncDisposable
{
    public virtual ValueTask DisposeAsync();
}

// new
public partial class StreamReader : TextReader
{
    public override ValueTask DisposeAsync();
}

// existing
public partial class StreamWriter : TextWriter
{
    public override ValueTask DisposeAsync();
}

API Usage

await using var reader = new StreamReader(myAsyncStream);

Alternative Designs

No response

Risks

No response

Activity

  1. added
    api-suggestion
    on Jun 30, 2023
  2. ghost added
    untriaged
    on Jun 30, 2023
  3. changed the title [-][API Proposal]: Implement IAsyncDisposable on `TextReader` and `TextWriter`[/-] [+][API Proposal]: Implement IAsyncDisposable on `TextReader`[/+] on Jun 30, 2023
  4. added
    api-ready-for-review
    and removed
    api-suggestion
    untriaged
    on Jun 30, 2023
  5. self-assigned this
    on Jun 30, 2023
  6. added this to the 8.0.0 milestone on Jun 30, 2023
  7. adamsitnik commented on Jun 30, 2023

    @adamsitnik
    Member

    Hi @Neme12

    Thank you for your proposal. It sounds very reasonable to me, so I've marked it as ready for review. It should get reviewed by our API Review Board within few weeks.

  8. terrajobst commented on Jul 18, 2023

    @terrajobst
    Contributor

    Video

    • Looks good as proposed
    namespace System.IO;
    
    public partial class TextReader
    {
        public virtual ValueTask DisposeAsync();
    }
    
    public partial class StreamReader : TextReader
    {
        public override ValueTask DisposeAsync();
    }
  9. added
    api-approved
    and removed
    api-ready-for-review
    on Jul 18, 2023
  10. removed their assignment
    on Jul 18, 2023
  11. 32 remaining items

  12. removed
    help wanted
    on Feb 15, 2024
  13. AndyBevan commented on Feb 18, 2024

    @AndyBevan
    Contributor

    @stephentoub - I was looking at picking this up to implement but I see you removed the "good first issue" and "help wanted" labels.

    Is this issue no longer looking for help on it?

  14. stephentoub commented on Feb 18, 2024

    @stephentoub
    Member

    Thanks, @AndyBevan. I removed the label because I'm still not convinced this is a necessary addition. Do you have an example case where it would be beneficial?

  15. AndyBevan commented on Feb 19, 2024

    @AndyBevan
    Contributor

    Hi @stephentoub - No I don't think I do have an example, I was looking at this as something to help out on but after re-reviewing the thread it sounds like this code change is no longer needed - I'll look for something else. Thanks for getting back to me.

  16. Neme12 commented on May 5, 2024

    @Neme12
    Author

    Regardless, from your comments / questions this all still sounds theoretical and that you don't actually have a known case where asynchronous work would be performed in a DisposeAsync on a Stream you're using underneath TextReader?

    @stephentoub I'm sorry, I assumed the disposal of the stream would be asynchronous in the example I provided:

    using var client = new HttpClient();
    await using var stream = await client.GetStreamAsync(url);

    I should have tried looking into the implementation details before opening this issue. However, I'd still argue there is value in this, since whether or not the returned stream in this case supports asynchronous disposal is indeed an implementation detail that I cannot infer by looking at the API, and that could in theory change in the future. The same is true for many other streams that come from various APIs. Stream is an abstraction. If I don't know whether a certain stream has async disposal or not, the safer thing to do is to always use DisposeAsync - at worst, it will just return synchronously and everything will be fine. If it's asynchronous however and I call the synchronous Dispose method, I could be doing sync-over-async, which is a big no-no.

  17. modified the milestones: 9.0.0, Future on Jul 18, 2024
  18. added 2 commits that reference this issue on Mar 29, 2026
    524b733
    aaf19ec
  19. danmoseley commented on Apr 9, 2026

    @danmoseley
    Contributor

    @adamsitnik happened to be looking at this as a test for an AI agent fixer. but reading discussion above, the argument is that "a known case where asynchronous work would be performed in a DisposeAsync on a Stream you're using underneath TextReader" is actually not clear. So allowing async dispose here would be a minor pit of perf failure, adding async machinery that is just overhead.

    I had Copilot just scan the dotnet/runtime codebase and it couldn't find an example where we would want to usefully use this -- unless perhaps it was for prettiness where perf didn't matter.

    This makes sense to me. Do you have another perspective or should we close this perhaps? I wasn't at the original discussion.

  20. jilles-sg commented on May 6, 2026

    @jilles-sg

    @danmoseley If it is best to use Dispose instead of DisposeAsync on a read-only Stream, can this be documented a bit more clearly?

  21. Neme12 commented on May 9, 2026

    @Neme12
    Author

    Yup, it's not clear at all that we shouldn't use DisposeAsync on a stream, even a file stream that was opened asynchronously, if it's used just for reading, so people already do this. Since this is not clear and I use DisposeAsync everywhere in cases where the Stream supports asynchronous operations, my two cents would be to align TextReader to what the underlying Stream supports, whether or not we found one that actually has async disposal - someone might have one, since everyone can implement a custom Stream. It's an abstraction, not a closed set of streams that exist in dotnet/runtime. And this issue does have a few upvotes. On TextReader, we have methods that forward to the abstraction, so it would make sense to have both forwarding methods.

  22. datvm commented on May 22, 2026

    @datvm

    In Blazor, a few Stream can only perform async and throw on non-async operations. Currently it's quite awkward:

      await using var stream = ...
      using StreamReader reader = new(stream, leaveOpen: true);
      // ...

    May I ask when this will be implemented?

  23. Neme12 commented on Jun 5, 2026

    @Neme12
    Author

    In Blazor, a few Stream can only perform async and throw on non-async operations. Currently it's quite awkward:

    await using var stream = ...
    using StreamReader reader = new(stream, leaveOpen: true);
    // ...
    May I ask when this will be implemented?

    I'd say that's a good enough reason to implement this if Blazor blocks synchronous Stream disposals, even if only used for reading. What do you think, @danmoseley @stephentoub ?

  24. Neme12 commented on Jun 21, 2026

    @Neme12
    Author

    Could someone please put the help-wanted label on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.IO

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions