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
[API Proposal]: Implement IAsyncDisposable on TextReader #88244
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.
changed the title [-][API Proposal]: Implement IAsyncDisposable on `TextReader` and `TextWriter`[/-][+][API Proposal]: Implement IAsyncDisposable on `TextReader`[/+]on Jun 30, 2023
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.
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?
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.
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:
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.
@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.
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.
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 ?
Activity
[-][API Proposal]: Implement IAsyncDisposable on `TextReader` and `TextWriter`[/-][+][API Proposal]: Implement IAsyncDisposable on `TextReader`[/+]adamsitnik commentedon Jun 30, 2023
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.
terrajobst commentedon Jul 18, 2023
Video
32 remaining items
AndyBevan commentedon Feb 18, 2024
@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?
stephentoub commentedon Feb 18, 2024
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?
AndyBevan commentedon Feb 19, 2024
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.
Neme12 commentedon May 5, 2024
@stephentoub I'm sorry, I assumed the disposal of the stream would be asynchronous in the example I provided:
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.
Streamis an abstraction. If I don't know whether a certain stream has async disposal or not, the safer thing to do is to always useDisposeAsync- at worst, it will just return synchronously and everything will be fine. If it's asynchronous however and I call the synchronousDisposemethod, I could be doing sync-over-async, which is a big no-no.Fix dotnet#88244: Implement IAsyncDisposable and DisposeAsync on Text…
Fix dotnet#88244: StreamReader implements IAsyncDisposable
danmoseley commentedon Apr 9, 2026
@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.
jilles-sg commentedon May 6, 2026
@danmoseley If it is best to use Dispose instead of DisposeAsync on a read-only Stream, can this be documented a bit more clearly?
Neme12 commentedon May 9, 2026
Yup, it's not clear at all that we shouldn't use
DisposeAsyncon 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 useDisposeAsynceverywhere in cases where the Stream supports asynchronous operations, my two cents would be to alignTextReaderto what the underlyingStreamsupports, 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. OnTextReader, we have methods that forward to the abstraction, so it would make sense to have both forwarding methods.datvm commentedon May 22, 2026
In Blazor, a few Stream can only perform async and throw on non-async operations. Currently it's quite awkward:
May I ask when this will be implemented?
Neme12 commentedon Jun 5, 2026
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 ?
Neme12 commentedon Jun 21, 2026
Could someone please put the help-wanted label on this?