dotnet / dotnet/aspnetcore

FileBufferingReadStream CopyToAsync Performance problem when wrapped

Open
#48,242 6 comments 0 reactions 0 assignees View on GitHub
area-networking
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

Previously reported #24032 and fixed in #24609.

When buffering a response using a wrapper around `FileBufferingReadStream`: initially, the stream's Position and Length are both 0. Based on the code in [Stream.cs](https://github.com/dotnet/runtime/blob/ca074078a6f87bf24e473e868f8ea0616ab8f356/src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs#L120-L156), CopyToAsync chooses a buffer size of 1.

Originally `CopyToAsync` was only fixed by updating the FileBufferingReadStream logic. But if we wrap the stream in another stream, the issue occurs again.

image

We are currently solving this issue by calling the overload of CopyToAsync that accepts an explicit buffer size.

### Expected Behavior

Expect the stream copy to use a default buffer of > 1 byte.

### Steps To Reproduce

```
var a = new TimedInputStream(fileBufferedReadStream);
await a.CopyToAsync(outputStream);
```

### Exceptions (if any)

_No response_

### .NET Version

_No response_

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.