-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Open
Description
Description
I discovered issue this while filing #88244 and looking at the source code. By default, StreamWriter.Dispose also disposes/closes the underlying Stream. DisposeAsync should close the underlying Stream asynchronously, but currently closes it synchronously.
See
runtime/src/libraries/System.Private.CoreLib/src/System/IO/StreamWriter.cs
Lines 241 to 257 in 2a22adb
| private async ValueTask DisposeAsyncCore() | |
| { | |
| // Same logic as in Dispose(), but with async flushing. | |
| Debug.Assert(GetType() == typeof(StreamWriter)); | |
| try | |
| { | |
| if (!_disposed) | |
| { | |
| await FlushAsync().ConfigureAwait(false); | |
| } | |
| } | |
| finally | |
| { | |
| CloseStreamFromDispose(disposing: true); | |
| } | |
| GC.SuppressFinalize(this); | |
| } |
and
runtime/src/libraries/System.Private.CoreLib/src/System/IO/StreamWriter.cs
Lines 211 to 234 in 2a22adb
| private void CloseStreamFromDispose(bool disposing) | |
| { | |
| // Dispose of our resources if this StreamWriter is closable. | |
| if (_closable && !_disposed) | |
| { | |
| try | |
| { | |
| // Attempt to close the stream even if there was an IO error from Flushing. | |
| // Note that Stream.Close() can potentially throw here (may or may not be | |
| // due to the same Flush error). In this case, we still need to ensure | |
| // cleaning up internal resources, hence the finally block. | |
| if (disposing) | |
| { | |
| _stream.Close(); | |
| } | |
| } | |
| finally | |
| { | |
| _disposed = true; | |
| _charLen = 0; | |
| base.Dispose(disposing); | |
| } | |
| } | |
| } |
Reproduction Steps
Expected behavior
Actual behavior
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response
Reactions are currently unavailable