Skip to content

StreamWriter.DisposeAsync disposes the underlying Stream synchronously #88246

Open

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

Activity

  1. ghost added
    untriaged
    on Jun 30, 2023
  2. Neme12 commented on Jul 3, 2023

    @Neme12
    Author

    I also think it should call stream.Dispose() instead of stream.Close(). If anyone creates their own stream and implements these methods differently, I think the expectation is that when you call streamReader.Dispose(), it would also call Dispose() on the stream. But that would be a breaking change of course (although fixing this bug about calling the sync vs async Close method is technically a breaking change too).

  3. dersia commented on Jul 22, 2023

    @dersia

    I will take care of this as part of #88244

  4. jozkee commented on Jul 24, 2023

    @jozkee
    Member

    I also think it should call stream.Dispose() instead of stream.Close()

    I think for compatiblity reasons we should continue calling Close().

  5. added this to the 8.0.0 milestone on Jul 24, 2023
  6. ghost removed
    untriaged
    on Jul 24, 2023
  7. dersia commented on Jul 24, 2023

    @dersia

    I also think it should call stream.Dispose() instead of stream.Close()

    I think for compatiblity reasons we should continue calling Close().

    yeah, stream.dispose() does not call Close on the stream, so that would be a behavioral change. I found some other issues on StreamWriter.Dispose(). Please see #88244 (comment)

  8. Neme12 commented on Jul 24, 2023

    @Neme12
    Author

    yeah, stream.dispose() does not call Close on the stream, so that would be a behavioral change

    Right. But fixing this issue would be a behavioral change too, so if it's OK to make that behavioral change, then why shouldn't it be OK to make this other small behavioral change too.

  9. jozkee commented on Jul 24, 2023

    @jozkee
    Member

    @Neme12 because changing DisposeAsync is not as impactful as changing Dispose which has been around for longer.

  10. modified the milestones: 8.0.0, 9.0.0 on Aug 8, 2023
  11. NinoFloris commented on Nov 27, 2023

    @NinoFloris
    Contributor

    We have an example in Npgsql https://github.com/npgsql/Npgsql/blob/main/src/Npgsql/NpgsqlRawCopyStream.cs#L353-L376 of a stream that should ideally take advantage of async if it's there.

    NpgsqlRawCopyStream is the base stream used in our StreamReader/Writer derived types (same file, see NpgsqlCopyTextReader/Writer)

    It seems its made very hard to have StreamWriter call DisposeAsync on the underlying stream, many methods do GetType() == typeof(StreamWriter) checks and CloseStreamDuringDispose etc are all private, the * truly * async FlushAsync implementation is also private.

    I would welcome any fixes or workarounds to get StreamReader/Writer to respect DisposeAsync.

  12. modified the milestones: 9.0.0, Future on Jul 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions