You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PipeReader.ReadAsync can return empty segments #30786
I just noticed that segments in a ReadResult can be empty:
I assumed that the first segment was never empty, so I often use result.Buffer.First.Span[0]; to access the first byte.
Is this intended behaviour? Can this be caused by me writing to the pipe in a wrong way? This is how I fill my pipe from a System.IO.Compression.DeflateStream:
@davidfowl@pakrym IIRC I believe this behavior can happen based on how the pipe advances internally. I remember you two had a conversation about it. Is it possible to check if the first segment is empty before returning the ReadResult?
Just FYI, this behaviour does not occur in my setup if I read the inflate stream's output into a dedicated buffer, and WriteAsync the buffer into the pipe.
Ugh yes we need to try to solve the empty segment problem. It’s a real PITA to deal with that today. Honestly I’d rather try to fix it with ReadOnlySequence itself but we’d need to measure the impact.
ROS is very performance sensitive and we would have to add empty segment skipping code to multiple very hot paths (construction, First, enumeration etc)
@jkotalik just making sure, you want me to upgrade to sdk preview9, but keep System.IO.Pipelines on the latest stable release (4.5.3)? Was 34765c5 landed in the sdk? I upgraded the SDK to 3.0.100-preview9-014004 and kept System.IO.Pipelines 4.5.3 but it happened again. I also noticed other segments can be empty too:
@davidfowl I am using new Pipe() with no explicit options (source).
@Trolldemorted if you can provide a repro (set of steps to reproduce the empty segments) that would be great. It would mean logging what you wrote and how you called AdvancedTo on the other side.
A repro for an empty first segment is tricky, as they happen pretty rarely (last time after >2GB of input). However, I found a small input for an empty non-first segment:
If you have VS and dotnet core 3 preview9 installed, opening it and pressing F5 should throw the "segment empty!" InvalidDataException, at least it does that reliably on my laptop.
so, what's the idiomatic way to get the first byte, since this is unsafe: ros.First.Span[0]
what about the second byte, is this also problematic? ros.Slice(1).First.Span[0]
So I upgraded the repro to use netcoreapp3.1 and the System.IO.Pipelines 6.0.3 and I get an empty segment at the end. When I upgrade to .NET 6 the problems goes away (likely from changes to the Stream).
PS: The code is also incorrect here, it should be:
if (result.Buffer.Length == 0 && (result.IsCanceled || result.IsCompleted))
We should fix any bugs that result in the PipeReader producing empty segments though. Narrowing down this repro on .NET Core 3.1 more might help figure out the exact reason why there's an empty segment at the end.
OK I have a repro. Generally when the length is passed into GetMemory we need to handle the case where's there's an existing segment allocated with nothing written into it, but more space is being requested than is available:
usingSystem;usingSystem.IO.Pipelines;usingSystem.Threading.Tasks;namespaceEmptySegmentsBug{classProgram{staticasyncTaskMain(string[]args){varpipe=newPipe();// Get a new block but don't write anythingvarbuffer=pipe.Writer.GetMemory();pipe.Writer.Advance(0);awaitpipe.Writer.FlushAsync();// Get a new blockbuffer=pipe.Writer.GetMemory(4096*2);pipe.Writer.Advance(buffer.Length);awaitpipe.Writer.FlushAsync();awaitReadAndDump();asyncTaskReadAndDump(){varresult=awaitpipe.Reader.ReadAsync();Console.Write("[");boolfirst=true;foreach(varsegmentinresult.Buffer){if(!first){Console.Write(",");}Console.Write(segment.Length);first=false;}Console.WriteLine("]");pipe.Reader.AdvanceTo(result.Buffer.End);}}}}
This prints: [0,8192]
ghost added
in-prThere is an active PR which will close this issue when it is merged
Activity
jkotalik commentedon Sep 6, 2019
@davidfowl @pakrym IIRC I believe this behavior can happen based on how the pipe advances internally. I remember you two had a conversation about it. Is it possible to check if the first segment is empty before returning the ReadResult?
Trolldemorted commentedon Sep 6, 2019
Just FYI, this behaviour does not occur in my setup if I read the inflate stream's output into a dedicated buffer, and
WriteAsyncthe buffer into the pipe.davidfowl commentedon Sep 6, 2019
Ugh yes we need to try to solve the empty segment problem. It’s a real PITA to deal with that today. Honestly I’d rather try to fix it with ReadOnlySequence itself but we’d need to measure the impact.
jkotalik commentedon Sep 6, 2019
What impact would it have?
pakrym commentedon Sep 6, 2019
ROS is very performance sensitive and we would have to add empty segment skipping code to multiple very hot paths (construction, First, enumeration etc)
davidfowl commentedon Sep 8, 2019
I think we should at the very least try to avoid empty segments while building up the structure internally.
pakrym commentedon Sep 8, 2019
Wonder if we should make it a rule, don't produce
ROS<>with empty segments. We won't be able to assert it unfortunately.jkotalik commentedon Sep 10, 2019
I think that rule works except when the
ROS<>is empty.@Trolldemorted From what I can tell, this potentially was fixed in 3.0. Could you try the latest 3.0-preview9 release and see if you hit this behavior again? https://dotnet.microsoft.com/download/dotnet-core/3.0.
edit: We believe dotnet/corefx@34765c5 should have fixed this issue. Please let us know if you are continuing to see this behavior.
davidfowl commentedon Sep 10, 2019
@Trolldemorted can you show how you created the Pipe? In particular I want to see the PipeOptions
Trolldemorted commentedon Sep 10, 2019
@jkotalik just making sure, you want me to upgrade to sdk preview9, but keep System.IO.Pipelines on the latest stable release (4.5.3)? Was 34765c5 landed in the sdk? I upgraded the SDK to 3.0.100-preview9-014004 and kept System.IO.Pipelines 4.5.3 but it happened again. I also noticed other segments can be empty too:

@davidfowl I am using
new Pipe()with no explicit options (source).davidfowl commentedon Sep 11, 2019
@Trolldemorted you need to update the package to preview9 of System.IO.Pipelines
Trolldemorted commentedon Sep 11, 2019
I upgraded to 4.6.0-preview9.19421.4, but it still happens. Might be my imagination but it happens less frequently, though.
davidfowl commentedon Sep 11, 2019
@Trolldemorted if you can provide a repro (set of steps to reproduce the empty segments) that would be great. It would mean logging what you wrote and how you called
AdvancedToon the other side.Trolldemorted commentedon Sep 11, 2019
A repro for an empty first segment is tricky, as they happen pretty rarely (last time after >2GB of input). However, I found a small input for an empty non-first segment:
https://github.com/Trolldemorted/EmptySegmentsBug/blob/371c7ceca7412f4313257c720965162a1b56ae76/EmptySegmentsBug/Program.cs
If you have VS and dotnet core 3 preview9 installed, opening it and pressing F5 should throw the "segment empty!" InvalidDataException, at least it does that reliably on my laptop.
Thanks a lot for your patience!
marklio commentedon Sep 11, 2019
Marking this as reliability, as First.Span[0] and FirstSpan[0] are extremely common when processing ROS.
9 remaining items
Trolldemorted commentedon Oct 9, 2020
A year has past, can you estimate when this will be fixed? I see you removed it from the 5.0 milestone, which is unfortunate.
freddyrios commentedon Feb 9, 2022
so, what's the idiomatic way to get the first byte, since this is unsafe:
ros.First.Span[0]what about the second byte, is this also problematic?
ros.Slice(1).First.Span[0]Trolldemorted commentedon Jul 18, 2022
any chance we get a fix for 7.0?
davidfowl commentedon Jul 18, 2022
@Trolldemorted How did you reproduce it?
Trolldemorted commentedon Jul 19, 2022
I have built a repro project here: https://github.com/Trolldemorted/EmptySegmentsBug/tree/371c7ceca7412f4313257c720965162a1b56ae76/EmptySegmentsBug
davidfowl commentedon Jul 19, 2022
So I upgraded the repro to use netcoreapp3.1 and the System.IO.Pipelines 6.0.3 and I get an empty segment at the end. When I upgrade to .NET 6 the problems goes away (likely from changes to the Stream).
PS: The code is also incorrect here, it should be:
We should fix any bugs that result in the PipeReader producing empty segments though. Narrowing down this repro on .NET Core 3.1 more might help figure out the exact reason why there's an empty segment at the end.
davidfowl commentedon Jul 19, 2022
OK I have a repro. Generally when the length is passed into GetMemory we need to handle the case where's there's an existing segment allocated with nothing written into it, but more space is being requested than is available:
This prints:
[0,8192]