dotnet / dotnet/yarp

Improve the exception for double HttpContent reads

Open
#1,683 5 comments 0 reactions 0 assignees View on GitHub
Type: Bug
Dominant language
C#
Stars
9.6k
Forks
933
Avg merge
12d 18h
Merged PRs (30d)
2

Description

We have logic that will detect if `HttpClient` attempts to read an `HttpContent` twice.

https://github.com/microsoft/reverse-proxy/blob/d789a3285b7c6d1769e39d472753bbd1b12a0966/src/ReverseProxy/Forwarder/StreamCopyHttpContent.cs#L134

The exception we currently throw is not helpful for the user to determine when this may happen and how they should react.
This may occur in a perfectly well-behaved proxy with no user error. While some cases may indicate a [bug](https://github.com/dotnet/runtime/issues/53914) in `SocketsHttpHandler`, there are also completely valid scenarios:

With HTTP/2, for example, we could have already started sending the request body to the server when the server responds with a GOAWAY. The server can tell us that it never processed the request, so HttpClient knows it can try to retry without side effects to the server application.
Some basic HttpContent types like StringContent support retrying like this seamlessly since they can rewind to the start of the body.
Yarp, however, does not buffer the request body. There's no way for us to rewind once we've started reading the body. Because of that, we can't seamlessly retry in this case, but Yarp's client likely can (and should).

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.