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

FileBufferingReadStream doesn't support 0-byte reads #41287

Open
1 task done
Tratcher opened this issue Apr 20, 2022 · 0 comments
Open
1 task done

FileBufferingReadStream doesn't support 0-byte reads #41287

Tratcher opened this issue Apr 20, 2022 · 0 comments
Assignees
Labels
area-runtime good first issue help wanted

Comments

@Tratcher
Copy link
Member

@Tratcher Tratcher commented Apr 20, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

The 0-byte read pattern does not work with FileBufferingReadStream.

On first read with a zero-length buffer it returns 0, but that causes the stream to think it's completely buffered and to stop reading from the inner stream.

if (read > 0)
{
await _buffer.WriteAsync(buffer.Slice(0, read), cancellationToken);
}
else
{
_completelyBuffered = true;
}

Expected Behavior

0-byte reads should return 0, but subsequent reads should still return data.

Steps To Reproduce


    [Fact]
    public async Task FileBufferingReadStream_Async0ByteReadUnderThreshold_DoesntCreateFile()
    {
        var inner = MakeStream(1024);
        using (var stream = new FileBufferingReadStream(inner, 1024 * 2, null, Directory.GetCurrentDirectory()))
        {
            var bytes = new byte[1000];
            var read0 = await stream.ReadAsync(bytes, 0, 0);
            Assert.Equal(0, read0);
            Assert.Equal(read0, stream.Length);
            Assert.Equal(read0, stream.Position);
            Assert.True(stream.InMemory);
            Assert.Null(stream.TempFileName);

            var read1 = await stream.ReadAsync(bytes, 0, bytes.Length);
            Assert.Equal(bytes.Length, read1);
            Assert.Equal(read0 + read1, stream.Length);
            Assert.Equal(read0 + read1, stream.Position);
            Assert.True(stream.InMemory);
            Assert.Null(stream.TempFileName);

            var read2 = await stream.ReadAsync(bytes, 0, bytes.Length);
            Assert.Equal(inner.Length - read0 - read1, read2);
            Assert.Equal(read0 + read1 + read2, stream.Length);
            Assert.Equal(read0 + read1 + read2, stream.Position);
            Assert.True(stream.InMemory);
            Assert.Null(stream.TempFileName);

            var read3 = await stream.ReadAsync(bytes, 0, bytes.Length);
            Assert.Equal(0, read3);
        }
    }

Exceptions (if any)

No response

.NET Version

6.0

Anything else?

Discovered by YARP users: microsoft/reverse-proxy#1657

@Tratcher Tratcher self-assigned this Apr 20, 2022
@Tratcher Tratcher added help wanted area-runtime good first issue labels Apr 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-runtime good first issue help wanted
Projects
None yet
Development

No branches or pull requests

1 participant