Skip to content

StreamWriter.DisposeAsync disposes the underlying Stream synchronously #88246

@Neme12

Description

@Neme12

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

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
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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions