From 22e841b85d8d1182a244bae770d32ba4af9e40d7 Mon Sep 17 00:00:00 2001 From: Kuinox Date: Fri, 3 Dec 2021 00:57:58 +0100 Subject: [PATCH 1/2] Fixes #62167. WriteAsync was bugged when writing multiple segments if called after an .Advance(int) --- .../src/System/IO/Pipelines/Pipe.cs | 2 +- .../System.IO.Pipelines/tests/PipeLengthTests.cs | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/Pipe.cs b/src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/Pipe.cs index f90ac5b8a9b9fe..0149c04051d91c 100644 --- a/src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/Pipe.cs +++ b/src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/Pipe.cs @@ -1011,7 +1011,7 @@ private void WriteMultiSegment(ReadOnlySpan source) } // We filled the segment - _writingHead.End += writable; + _writingHead.End += _writingHeadBytesBuffered; _writingHeadBytesBuffered = 0; // This is optimized to use pooled memory. That's why we pass 0 instead of diff --git a/src/libraries/System.IO.Pipelines/tests/PipeLengthTests.cs b/src/libraries/System.IO.Pipelines/tests/PipeLengthTests.cs index fcf9d7af55b053..fd9fc83833a8b1 100644 --- a/src/libraries/System.IO.Pipelines/tests/PipeLengthTests.cs +++ b/src/libraries/System.IO.Pipelines/tests/PipeLengthTests.cs @@ -277,5 +277,19 @@ public async Task NullExaminedAndConsumedNoops() ReadResult result = await _pipe.Reader.ReadAsync(); _pipe.Reader.AdvanceTo(default, default); } + + [Fact] + public async Task AdvanceFollowedByWriteAsyncTest() + { + Memory buffer = new byte[26]; + Pipe pipe = new(new PipeOptions(minimumSegmentSize: 1)); + + var mem = pipe.Writer.GetMemory(14)[..14]; + buffer[..14].CopyTo(mem); + pipe.Writer.Advance(14); + await pipe.Writer.WriteAsync(buffer[14..]); + ReadResult res = await pipe.Reader.ReadAsync(); + Assert.Equal(res.Buffer.Length, buffer.Length); + } } } From a427a029bc59c034cb7fdfbd5d45d7a1076771a9 Mon Sep 17 00:00:00 2001 From: Santiago Fernandez Madero Date: Sun, 2 Jan 2022 20:49:51 -0600 Subject: [PATCH 2/2] Add packaging changes --- src/libraries/System.IO.Pipelines/Directory.Build.props | 2 +- src/libraries/libraries-packages.proj | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.IO.Pipelines/Directory.Build.props b/src/libraries/System.IO.Pipelines/Directory.Build.props index 0daa67b02c97ba..81ab65eae172cf 100644 --- a/src/libraries/System.IO.Pipelines/Directory.Build.props +++ b/src/libraries/System.IO.Pipelines/Directory.Build.props @@ -2,7 +2,7 @@ Open - 1 + 2 5.0.0.1 diff --git a/src/libraries/libraries-packages.proj b/src/libraries/libraries-packages.proj index 87660e3c2483c9..00d67ec9881279 100644 --- a/src/libraries/libraries-packages.proj +++ b/src/libraries/libraries-packages.proj @@ -18,6 +18,7 @@ +