Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pkg/Microsoft.Private.PackageBaseline/packageIndex.json
Original file line number Diff line number Diff line change
Expand Up @@ -2567,15 +2567,17 @@
"4.5.2",
"4.5.3",
"4.6.0",
"4.7.0"
"4.7.0",
"4.7.1"
],
"BaselineVersion": "4.7.0",
"InboxOn": {},
"AssemblyVersionInPackageVersion": {
"4.0.0.0": "4.5.0",
"4.0.0.1": "4.5.2",
"4.0.1.0": "4.6.0",
"4.0.2.0": "4.7.0"
"4.0.2.0": "4.7.0",
"4.0.2.1": "4.7.1"
}
},
"System.IO.Pipes": {
Expand Down
3 changes: 2 additions & 1 deletion src/System.IO.Pipelines/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project>
<Import Project="..\Directory.Build.props" />
<PropertyGroup>
<AssemblyVersion>4.0.2.0</AssemblyVersion>
<AssemblyVersion>4.0.2.1</AssemblyVersion>
<PackageVersion>4.7.1</PackageVersion>
<StrongNameKeyId>Open</StrongNameKeyId>
</PropertyGroup>
</Project>
17 changes: 7 additions & 10 deletions src/System.IO.Pipelines/src/System/IO/Pipelines/PipeReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,13 @@ private async Task CopyToAsyncCore<TStream>(TStream destination, Func<TStream, R
{
while (true)
{
SequencePosition consumed = default;

ReadResult result = await ReadAsync(cancellationToken).ConfigureAwait(false);
ReadOnlySequence<byte> buffer = result.Buffer;
SequencePosition position = buffer.Start;
SequencePosition consumed = position;

try
{
ReadOnlySequence<byte> buffer = result.Buffer;
SequencePosition position = buffer.Start;

if (result.IsCanceled)
{
ThrowHelper.ThrowOperationCanceledException_ReadCanceled();
Expand All @@ -196,11 +195,9 @@ private async Task CopyToAsyncCore<TStream>(TStream destination, Func<TStream, R
consumed = position;
}

if (consumed.Equals(default))
{
consumed = buffer.End;
}

// The while loop completed successfully, so we've consumed the entire buffer.
consumed = buffer.End;

if (result.IsCompleted)
{
break;
Expand Down
84 changes: 84 additions & 0 deletions src/System.IO.Pipelines/tests/PipeReaderCopyToAsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,95 @@ public async Task ThrowingFromStreamDoesNotLeavePipeReaderInBrokenState()
Assert.Equal(20, result.Buffer.Length);
pipe.Reader.Complete();
}

[Theory]
[InlineData(0)]
[InlineData(1)]
public async Task ThrowingFromStreamCallsAdvanceToWithStartOfLastReadResult(int throwAfterNWrites)
{
var pipe = new Pipe(s_testOptions);
var wrappedPipeReader = new TestPipeReader(pipe.Reader);

var stream = new ThrowAfterNWritesStream(throwAfterNWrites);
Task task = wrappedPipeReader.CopyToAsync(stream);

pipe.Writer.WriteEmpty(10);
await pipe.Writer.FlushAsync();

// Write twice for the test case where the stream throws on the second write.
pipe.Writer.WriteEmpty(10);
await pipe.Writer.FlushAsync();

await Assert.ThrowsAsync<InvalidOperationException>(() => task);

SequencePosition startPosition = wrappedPipeReader.LastReadResult.Buffer.Start;

Assert.NotNull(startPosition.GetObject());
Assert.True(startPosition.Equals(wrappedPipeReader.LastConsumed));
Assert.True(startPosition.Equals(wrappedPipeReader.LastExamined));
}

private class ThrowingStream : ThrowAfterNWritesStream
{
public ThrowingStream() : base(0)
{
}
}

private class TestPipeReader : PipeReader
{
private readonly PipeReader _inner;


public TestPipeReader(PipeReader inner)
{
_inner = inner;
}

public ReadResult LastReadResult { get; private set; }
public SequencePosition LastConsumed { get; private set; }
public SequencePosition LastExamined { get; private set; }

public override void AdvanceTo(SequencePosition consumed)
{
LastConsumed = consumed;
LastExamined = consumed;
_inner.AdvanceTo(consumed);
}

public override void AdvanceTo(SequencePosition consumed, SequencePosition examined)
{
LastConsumed = consumed;
LastExamined = examined;
_inner.AdvanceTo(consumed);
}

public override void CancelPendingRead()
{
_inner.CancelPendingRead();
}

public override void Complete(Exception exception = null)
{
_inner.Complete(exception);
}

public override async ValueTask<ReadResult> ReadAsync(CancellationToken cancellationToken = default)
{
LastReadResult = await _inner.ReadAsync(cancellationToken);
return LastReadResult;
}

public override bool TryRead(out ReadResult result)
{
if (_inner.TryRead(out result))
{
LastReadResult = result;
return true;
}

return false;
}
}
}
}
3 changes: 3 additions & 0 deletions src/packages.builds
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
<Project Include="$(MSBuildThisFileDirectory)System.Runtime.CompilerServices.Unsafe\pkg\System.Runtime.CompilerServices.Unsafe.pkgproj">
<AdditionalProperties>$(AdditionalProperties)</AdditionalProperties>
</Project>
<Project Include="$(MSBuildThisFileDirectory)System.IO.Pipelines\pkg\System.IO.Pipelines.pkgproj">
<AdditionalProperties>$(AdditionalProperties)</AdditionalProperties>
</Project>
</ItemGroup>

<ItemGroup>
Expand Down