Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PauseWriterThreshold and ResumeWriterThreshold options of PipeOptions not validated properly #75166

Closed
alhimik45 opened this issue Sep 6, 2022 · 3 comments · Fixed by #77359
Labels
area-System.IO.Pipelines help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@alhimik45
Copy link

Description

It is possible to pass values of ResumeWriterThreshold and PauseWriterThreshold that causes program to hang. There is a check that disallows ResumeWriterThreshold to be greater than PauseWriterThreshold (code), but it doesn't work in case when ResumeWriterThreshold left to default -1 value and PauseWriterThreshold passed explicitly and less than DefaultResumeWriterThreshold. In this case options contains ResumeWriterThreshold that greater than PauseWriterThreshold and writer resume never happens.

Similar situation come when passed ResumeWriterThreshold equal to 0. In this case this check is not true even if _unconsumedBytes is zero and writer is not resumed. Not sure is it lack of validation of wrong options value or bug in code that should work with zero value.

Reproduction Steps

Here is small program that successfully completes with correct values and hangs forever in cases described above

using System.Buffers;
using System.IO.Pipelines;
using System.Text;

//correct values for immediate backpressure
// var pipe = new Pipe(new PipeOptions(pauseWriterThreshold: 1, resumeWriterThreshold: 1));

//using default resumeWriterThreshold gives incorrect combination of values
var pipe = new Pipe(new PipeOptions(pauseWriterThreshold: 1, resumeWriterThreshold: -1));

//with zero resumeWriterThreshold program also hangs
var pipe = new Pipe(new PipeOptions(pauseWriterThreshold: 1, resumeWriterThreshold: 0));

var readTask = Task.Run(async () =>
{
    ReadResult read;
    do
    {
        read = await pipe.Reader.ReadAsync();
        Console.WriteLine($"Read {Encoding.UTF8.GetString(read.Buffer.ToArray())}");
        pipe.Reader.AdvanceTo(read.Buffer.End);
    } while (!read.IsCompleted);
});

var writeTask = Task.Run(async () =>
{
    await pipe.Writer.WriteAsync(Encoding.UTF8.GetBytes("qwerty1"));
    await Task.Delay(100);
    await pipe.Writer.WriteAsync(Encoding.UTF8.GetBytes("qwerty2"));
    await pipe.Writer.CompleteAsync();
});

await Task.WhenAll(readTask, writeTask);

Expected behavior

PipeOptions constructor should throw ArgumentOutOfRangeException for such invalid values

Actual behavior

PipeWriter not resumed after pause

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Sep 6, 2022
@wtgodbe wtgodbe added this to the 8.0.0 milestone Sep 7, 2022
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Sep 7, 2022
@wtgodbe wtgodbe added the help wanted [up-for-grabs] Good issue for external contributors label Sep 7, 2022
@wtgodbe
Copy link
Member

wtgodbe commented Sep 7, 2022

Thanks for the report - we think this is a good candidate for a community contribution, so applying the help wanted label

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Oct 23, 2022
@cdbullard
Copy link
Contributor

Hi all,
I have opened a PR that I believe should correct this behavior, please let me know if anything was missed or if any changes might be required.

@cdbullard
Copy link
Contributor

Hello,
Just wanted to check in to see if anyone was able to review my PR, let me know if there are any questions. Thanks!

@ghost ghost added in-pr There is an active PR which will close this issue when it is merged and removed in-pr There is an active PR which will close this issue when it is merged labels Jan 27, 2023
@ghost ghost added in-pr There is an active PR which will close this issue when it is merged and removed in-pr There is an active PR which will close this issue when it is merged labels Oct 16, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Nov 4, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Dec 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.IO.Pipelines help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants