Runtime: ReadOnlySequence<T> seems to hand out incorrect position

Created on 26 Jan 2020  路  14Comments  路  Source: dotnet/runtime

When stress testing a bedrock client, I have an error that occurs frequently. Thanks to the hard work of @davidfowl this has been isolated to an issue in the BCL. The error that occurs is below

Unhandled exception. System.InvalidOperationException: End position was not reached during enumeration.
at System.ThrowHelper.ThrowInvalidOperationException_EndPositionNotReached()
at System.Buffers.SequenceReader1.IsNextSlow(ReadOnlySpan1 next, Boolean advancePast)
at System.Buffers.SequenceReader1.TryReadTo(ReadOnlySequence1& sequence, ReadOnlySpan1 delimiter, Boolean advancePastDelimiter) at Bedrock.Framework.Protocols.Http1ResponseMessageReader.TryParseMessage(ReadOnlySequence1& input, SequencePosition& consumed, SequencePosition& examined, HttpResponseMessage& message) in C:\code\BedrockFramework\src\Bedrock.Framework.Experimental\Protocols\Http1ResponseMessageReader.cs:line 62

The minimal repro for this (again thanks to @davidfowl ) is

```C#
var segment = new Segment(new byte[] { 1, 2, 3 });
var segment2 = new Segment(new byte[] { 4, 5, 6 });
segment.NextSegment = segment2;

var reader = new SequenceReader(new ReadOnlySequence(segment, 0, segment, 1));
reader.IsNext(new byte[] { 1, 2 });

public class Segment : ReadOnlySequenceSegment
{
public Segment(byte[] data) => Memory = data;
public Segment NextSegment { get => (Segment)Next; set => Next = value; }
}
```

area-System.Buffers bug

Most helpful comment

Does this seem to be a duplicate of what is fixed by #276

Yes. I just tested the above test and it has been fixed in master. We should backport the fix as part of a the release/3.1 serivcing.

All 14 comments

Does this seem to be a duplicate of what is fixed by #276

@Drawaes that stack trace is exactly like mine, and i was stress testing my code when it happened.

Can you test your code under a netcoreapp5.0 preview?
That fixed it for me(although i can't take a preview to production :/)

Does this seem to be a duplicate of what is fixed by #276

Yes. I just tested the above test and it has been fixed in master. We should backport the fix as part of a the release/3.1 serivcing.

Waiting to close until https://github.com/dotnet/runtime/pull/276 has been ported.

Removed Servicing Consider. We should only use that on PRs ready to merge.
@JeremyKuhne can open a release/3.1 PR for this and add the template?

can open a release/3.1 PR for this and add the template?

Sure.

I just hit this problem in 3.1.
What's the best workaround?

@raffaeler see this comment for a workaround.

Thank you @AlgorithmsAreCool
BTW I believe to have resolved by not trying to read the data until the minimum amount of bytes is available. I tried with the PipeOptions to set this minimum amount, but didn't work for some reason. Not the best fix, but looks like working.

What's the best workaround?

@raffaeler - you could wait for the next servicing release of 3.1 which contains the fix (once this is merged: https://github.com/dotnet/corefx/pull/42845).

Look out for it in 3.1.3.

Thanks @ahsonkhan but I am working on the code and I am can't stop the development.
Anyway I will do the patch as soon as it is available.

@raffaeler fyi, the fix is already in master. I have a preview version of core 5.0 installed and just changed my project to target netcoreapp5.0 and it works.

installing a preview build is just as easy as installing any version of the core sdk.

Thanks @AlgorithmsAreCool I am already very familiar with preview releases

Fixed in https://github.com/dotnet/corefx/pull/42845 for 3.1 as well. Thanks @JeremyKuhne

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GitAntoinee picture GitAntoinee  路  3Comments

chunseoklee picture chunseoklee  路  3Comments

yahorsi picture yahorsi  路  3Comments

Timovzl picture Timovzl  路  3Comments

EgorBo picture EgorBo  路  3Comments