Description
In below snippet I was expecting an exception to be thrown but instead the StreamWriter keeps working:
var ms = new MemoryStream();
var sw = new StreamWriter(ms, leaveOpen: true);
sw.Write("Hello");
sw.Dispose();
sw.Write(" World");
sw.Flush();
Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray())); // prints "Hello World".If I change leaveOpen to false, it does throw as I expected.
Originally posted by @jozkee in #89477 (comment)
[Breaking Change] StreamWriter keeps working after being disposed if leaveOpen: true is used
We are fixing StreamWriter so that it correctly throws ObjectDisposedException after Dispose() / DisposeAsync() when constructed with leaveOpen: true. The fix is currently in PR #125189.
Related:
Goal
The goal is to align StreamWriter disposal behavior with StreamReader and standard IDisposable semantics. When leaveOpen: true, Dispose() should mark the writer as disposed (so subsequent operations throw) while leaving the underlying stream open. Currently, the _disposed flag is never set when leaveOpen: true, which means write and flush calls silently succeed after disposal.
Existing Behavior
When a StreamWriter is constructed with leaveOpen: true and then disposed, subsequent Write, Flush, and async variants continue to work without throwing any exception:
var ms = new MemoryStream();
var sw = new StreamWriter(ms, leaveOpen: true);
sw.Write("Hello");
sw.Dispose();
sw.Write(" World"); // No exception — writes succeed
sw.Flush();
Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
// prints "Hello World"Note: StreamReader with the same leaveOpen: true flag correctly throws ObjectDisposedException after disposal. This inconsistency has existed since the leaveOpen parameter was introduced.
Breaking Change
After this fix, all write and flush operations on a disposed StreamWriter throw ObjectDisposedException, regardless of the leaveOpen parameter:
var ms = new MemoryStream();
var sw = new StreamWriter(ms, leaveOpen: true);
sw.Write("Hello");
sw.Dispose();
sw.Write(" World"); // Now throws ObjectDisposedExceptionThe change is technically breaking because code that relied on the ability to write after disposal with leaveOpen: true will now throw at runtime. However, use-after-dispose is always documented as erroneous behavior in .NET, so we expect the practical impact to be low.
Error: System.ObjectDisposedException: Cannot write to a closed TextWriter.
Workaround: Remove usage patterns that write to a StreamWriter after calling Dispose() or DisposeAsync(). If you need to continue writing to the underlying stream, create a new StreamWriter wrapping it.
Affected APIs: All overloads of StreamWriter.Write, StreamWriter.WriteLine, StreamWriter.WriteAsync, StreamWriter.WriteLineAsync, StreamWriter.Flush, and StreamWriter.FlushAsync.
Feedback
We would love your feedback on this change. If you have scenarios where you depend on the current (pre-fix) behavior, please comment on this issue.
Activity
[-]StreamWriter doesn't throw after being disposed[/-][+]StreamWriter keeps working after being disposed if `leaveOpen: true` is used[/+]dersia commentedon Jul 28, 2023
Neme12 commentedon Jul 28, 2023
Note: This works as expected on
StreamReader, i.e. it throws after being disposed, regardless of the value ofleaveOpen.dersia commentedon Jul 28, 2023
Neme12 commentedon Jul 28, 2023
@dersia I meant
StreamReader. I fixed the comment.jozkee commentedon Jul 28, 2023
If we fix this someone somewhere will likely get broken, so we either don't fix it or we do and document it as a breaking change.
dersia commentedon Jul 28, 2023
Neme12 commentedon Jul 28, 2023
StreamReaderdoes throw when disposed:dersia commentedon Jul 28, 2023
dersia commentedon Jul 29, 2023
So I looked into the code and found out, why the Reader tests aren't throwing, although your testing throws. And I am not sure, if what I found is a as bad sign as I think it is. So basically the test are not really testing StreamReader, they are testing an internal derived class called
NullStreamReaderruntime/src/libraries/System.Private.CoreLib/src/System/IO/StreamReader.cs
Line 1412 in 4ed3550
So the Tests on
Stream.NullTest.cswon't test any of the Dispose logic ofStreamReader. So there are now 2 different things I am wondering about.Not sure which is the right option here. I think to be fully correct, one of the two need to happen as part of #89477 and right now I am leaning more towards the latter.
Although, this makes all test that involve Reading after dispose useless.
dersia commentedon Jul 29, 2023
As I was looking at this from my phone, I didn't realize that this is a separate issue, so don't mind this
jeffhandley commentedon Feb 8, 2026
Summary
When
StreamWriteris constructed withleaveOpen: true, callingDispose()does not mark the writer as disposed. SubsequentWriteandFlushcalls succeed instead of throwingObjectDisposedException. This is inconsistent withStreamReader, which correctly throws after disposal regardless ofleaveOpen. Filed by @jozkee (MS employee) and confirmed as a genuine inconsistency.Expected Fix
In
StreamWriter.Dispose(bool), whenleaveOpenis true, the method currently skips closing the underlying stream but also skips setting any "disposed" flag. The fix should set the internal_disposedflag (or null out the internal_streamfield like theleaveOpen: falsepath does) so that subsequent operations throwObjectDisposedException. Note: this is technically a breaking change since someone could be relying on the current behavior, but the team acknowledged it as a bug worth fixing.Test Coverage
leaveOpen: true, dispose it, then callWrite— should throwObjectDisposedException.DisposeAsync— should also throw on subsequent writes.StreamReaderwithleaveOpen: truealready throws after dispose (parity check).Repro
[-]StreamWriter keeps working after being disposed if `leaveOpen: true` is used[/-][+][Breaking Change] StreamWriter keeps working after being disposed if `leaveOpen: true` is used[/+]ViveliDuCh commentedon Mar 17, 2026
The consensus in #125189 is that the risk of breaking existing code outweighs the benefit given no active customer demand. Preserving the current behavior for backward compatibility.