Make Dispose not throw in StreamPipeWriter (#7376)
This commit is contained in:
parent
3bd5f2c2ab
commit
9e5f09cb44
|
|
@ -129,20 +129,7 @@ namespace System.IO.Pipelines
|
|||
return;
|
||||
}
|
||||
|
||||
_isCompleted = true;
|
||||
|
||||
_internalTokenSource?.Dispose();
|
||||
|
||||
if (_completedSegments != null)
|
||||
{
|
||||
foreach (var segment in _completedSegments)
|
||||
{
|
||||
segment.Return();
|
||||
}
|
||||
}
|
||||
|
||||
_currentSegmentOwner?.Dispose();
|
||||
|
||||
Dispose();
|
||||
// We still want to cleanup segments before throwing an exception.
|
||||
if (_bytesWritten > 0 && exception == null)
|
||||
{
|
||||
|
|
@ -286,7 +273,24 @@ namespace System.IO.Pipelines
|
|||
|
||||
public void Dispose()
|
||||
{
|
||||
Complete();
|
||||
if (_isCompleted)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_isCompleted = true;
|
||||
|
||||
_internalTokenSource?.Dispose();
|
||||
|
||||
if (_completedSegments != null)
|
||||
{
|
||||
foreach (var segment in _completedSegments)
|
||||
{
|
||||
segment.Return();
|
||||
}
|
||||
}
|
||||
|
||||
_currentSegmentOwner?.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -339,6 +339,25 @@ namespace System.IO.Pipelines.Tests
|
|||
Assert.Throws<InvalidOperationException>(() => Writer.GetSpan());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DisposeDoesNotThrowIfUnflushedData()
|
||||
{
|
||||
var streamPipeWriter = new StreamPipeWriter(new MemoryStream());
|
||||
streamPipeWriter.Write(new byte[1]);
|
||||
|
||||
streamPipeWriter.Dispose();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CompleteAfterDisposeDoesNotThrowIfUnflushedData()
|
||||
{
|
||||
var streamPipeWriter = new StreamPipeWriter(new MemoryStream());
|
||||
streamPipeWriter.Write(new byte[1]);
|
||||
|
||||
streamPipeWriter.Dispose();
|
||||
streamPipeWriter.Complete();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CallGetMemoryWithNegativeSizeHint_ThrowsArgException()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue