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: 0 additions & 6 deletions src/System.IO.Pipelines/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,6 @@
<data name="AdvanceToInvalidCursor" xml:space="preserve">
<value>The PipeReader has already advanced past the provided position.</value>
</data>
<data name="CannotCompleteWhileReading" xml:space="preserve">
<value>Can't complete reader while reading.</value>
</data>
<data name="CannotCompleteWhiteWriting" xml:space="preserve">
<value>Can't complete writer while writing.</value>
</data>
<data name="ConcurrentOperationsNotSupported" xml:space="preserve">
<value>Concurrent reads or writes are not supported.</value>
</data>
Expand Down
19 changes: 11 additions & 8 deletions src/System.IO.Pipelines/src/System/IO/Pipelines/Pipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -934,16 +934,19 @@ internal ValueTask<FlushResult> WriteAsync(ReadOnlyMemory<byte> source, Cancella
// state as writing
AllocateWriteHeadIfNeeded(0);

if (source.Length <= _writingHeadMemory.Length)
lock (_sync)
{
source.CopyTo(_writingHeadMemory);
if (source.Length <= _writingHeadMemory.Length)
{
source.CopyTo(_writingHeadMemory);

AdvanceCore(source.Length);
}
else
{
// This is the multi segment copy
WriteMultiSegment(source.Span);
AdvanceCore(source.Length);
}
else
{
// This is the multi segment copy
WriteMultiSegment(source.Span);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any concern of acquiring and releasing the lock immediately between here and FlushAsync? Can a call interleave in here that would affect the write call? I doubt it, but it's the only cause for concern I see.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fine. Efficiency.

}

return FlushAsync(cancellationToken);
Expand Down
28 changes: 28 additions & 0 deletions src/System.IO.Pipelines/tests/PipeWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,5 +232,33 @@ public async Task WritesUsingGetMemoryWorks()

pipe.Reader.Complete();
}

[Fact]
public async Task CompleteWithLargeWriteThrows()
{
var pipe = new Pipe();
pipe.Reader.Complete();

var task = Task.Run(async () =>
{
await Task.Delay(10);
pipe.Writer.Complete();
});

try
{
for (int i = 0; i < 1000; i++)
{
var buffer = new byte[10000000];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Allocate this array outside of the try.

await pipe.Writer.WriteAsync(buffer);
}
}
catch (InvalidOperationException)
{
// Complete while writing
}

await task;
}
}
}