High number of 502s - The response ended prematurely while waiting for the next frame from the server.
Describe the bug
We've recently introduced YARP into our infrastructure and over the past few days we've run into an issue where the proxy seemingly hits some sort of limit (we assume) and starts returning HTTP 502 errors to users.
For example, the graph below shows YARP request logs by status code for a single API endpoint. This particular endpoint is polled by a single user every fifteen minutes, typically making ~1600 requests with a request every ~200ms.

We can see that after so many requests we start returning HTTP 502 errors. Our traces tell us those 502s aren't making it to the backend, and they all fail with this error:
System.Net.Http.HttpRequestException: An error occurred while sending the request.
System.IO.IOException: The request was aborted.
System.IO.IOException: The response ended prematurely while waiting for the next frame from the server.
at System.Net.Http.Http2Connection.<ReadFrameAsync>g__ThrowMissingFrame|57_1()
at System.Net.Http.Http2Connection.ReadFrameAsync(Boolean initialFrame)
at System.Net.Http.Http2Connection.ProcessIncomingFramesAsync()
--- End of inner exception stack trace ---
at System.Net.Http.Http2Connection.ThrowRequestAborted(Exception innerException)
at System.Net.Http.Http2Connection.Http2Stream.CheckResponseBodyState()
at System.Net.Http.Http2Connection.Http2Stream.TryEnsureHeaders()
at System.Net.Http.Http2Connection.Http2Stream.ReadResponseHeadersAsync(CancellationToken cancellationToken)
at System.Net.Http.Http2Connection.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.Http2Connection.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.DiagnosticsHandler.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
Whilst the significant majority (99.9%) of requests returning 502s are for this single endpoint, we've noticed a small spike in 502s across other endpoints during the same time frame.
It's also worth mentioning that these 502s make up a small portion of the total number of requests that YARP is handling, meaning it doesn't appear to be any sort of global limit that is hit, otherwise we'd expect the majority of requests to fail.
Updating the routing so this particular endpoint bypasses YARP and directly hits the backend "resolves" the problem, and removes the increase in 502s we see across other endpoints but obviously this isn't a permanent solution, and so we're looking for any insight / advice around what may be causing this, or ideas around how we can troubleshoot further.
To Reproduce
We've attempted to recreate this both locally and in the same production environment, by using tools like JMeter to send a high volume of requests to a single endpoint, however none of these tests seem to trigger HTTP 502 errors.
Further technical details
Yarp Host: Nuget Package Version: 1.1.1. (Yarp.ReverseProxy) .NET Version: .NET 6.0
Deployment Environment:
- Azure FrontDoor configured to send requests to YARP.
- YARP - Linux Azure App Service (P2V3). Http version is set to 2.0, Http 2.0 Proxy setting is currently disabled.
- Backend service - Linux App Service running in the same regional VNET with private endpoints (to remove any possibility of something like SNAT).
YARP Config:
RouteConfigs = new List<RouteConfig>
{
new()
{
RouteId = "publicApi",
ClusterId = "publicApiCluster",
Match = new RouteMatch
{
Path = "{**catch-all}"
}
}
};
ClusterConfigs = new List<ClusterConfig>
{
new()
{
ClusterId = "publicApiCluster",
HealthCheck = new()
{
Active = new ActiveHealthCheckConfig
{
Enabled = true,
Interval = TimeSpan.FromSeconds(60),
Timeout = TimeSpan.FromSeconds(20),
Policy = HealthCheckConstants.ActivePolicy.ConsecutiveFailures,
Path = "/status"
}
},
HttpRequest = new()
{
ActivityTimeout = TimeSpan.FromSeconds(120)
},
Destinations = new Dictionary<string, DestinationConfig>
{
{
"destination",
new DestinationConfig { Address = $"https://xxx.com" }
}
}
}
};
That stack trace implies the destination is closing the connection before it's sent the response headers for the current request. You would need logs from the destination server to understand why it's doing that. A network trace should confirm it was the destination server that closed the connection.
Wouldn't that lead to the same issue now that the routing is going directly to the 'offending' server? If it was the destination server of the proxy, it wouldn't matter what proxy we put in-front of it, front-door or YARP, no?
One of the issues is that our destination server does not appear to be receiving the request at all for these 502s. This might be the nature of the PaaS infra, possible the App Service Plan load balancer is receiving the request and it's failing before forwarding to our App Service. As far as our logging can see this is all happening in the YARP client.
Does that backend support Http/2? App service has been rolling out their Http/2 support, but it may not have made it to the particular stamp that the specific site is running on. How does it behave if you use curl or postman and force http/2 connections to that host?
The destination may be rejecting something in the proxied messages, L7 proxying is never completely transparent. Sam's suggestion about HTTP/2 is a good example.
Update: it seems that the failing request had a very large header value. We could reliably get YARP to throw with a header of ~16KB. Which I would expect it to reject, but maybe with a more meaningful error. We are trying to find a minimum failing test for the header size. The offending header was an accept header with a large number of repeats (application/json,application/json,application/json etc), not sure if the error was the repetition or size of the header value.
Ok, this seems related to https://github.com/dotnet/aspnetcore/issues/17861, https://github.com/dotnet/aspnetcore/issues/12213, https://github.com/dotnet/aspnetcore/issues/44643, . Yes, Kestrel could report a more meaningful error to the client. We'll see about improving that.
We've identified a few different possible improvements for this behavior in the linked issues above. Closing this as external as no changes are expected in YARP.