microsoft / reverse-proxy

A toolkit for developing high-performance HTTP reverse proxy applications.
https://microsoft.github.io/reverse-proxy
MIT License
8.48k stars 836 forks source link

YARP keep terminating the WebSocket after around 2 minutes #2615

Open AbdallahR99 opened 1 week ago

AbdallahR99 commented 1 week ago

When I connect to the web socket through the original site, there is no problem in connecting and it lasts for a long time

But when I connect through YARP reverse proxy , this error appears after around 2 minutes:

UpgradeActivityTimeout: The WebSocket connection was closed after being idle longer than the Activity Timeout.

System.OperationCanceledException: The operation was canceled.

   at System.Threading.CancellationToken.ThrowOperationCanceledException()

   at System.IO.Pipelines.Pipe.GetReadAsyncResult()

   at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.ReadAsync(Memory`1 memory, CancellationToken cancellationToken)

   at Microsoft.AspNetCore.Server.IIS.Core.HttpRequestStream.ReadAsyncInternal(Memory`1 buffer, CancellationToken cancellationToken)

   at Yarp.ReverseProxy.Forwarder.StreamCopier.CopyAsync(Stream input, Stream output, Int64 promisedContentLength, StreamCopierTelemetry telemetry, ActivityCancellationTokenSource activityToken, Boolean autoFlush, CancellationToken cancellation)

I also noticed that yarp receive the ws calls as http

Screenshot 2024-09-24 091748

image

MihaZupan commented 1 week ago

YARP uses an ActivityTimeout with a 100-second default on all requests. See https://microsoft.github.io/reverse-proxy/articles/http-client-config.html#httprequest

The way to avoid it triggering for long-lived requests like WebSockets is to enable keep-alive/ping mechanisms.

AbdallahR99 commented 5 days ago

@MihaZupan modified the ActivityTimeout and still facing the same issue tried also to override the keep-alive in the headers and nothing changed

MihaZupan commented 5 days ago

You're seeing messages about The WebSocket connection was closed after being idle longer than the Activity Timeout. after two minutes even after increasing the timeout?

Are you able to create a small runnable repro?

Keep-Alive headers won't affect this. What I was referring to are actual periodic pings sent by the WebSocket client and/or server. E.g. if the client is using ClientWebSocket, you can change the KeepAliveInterval.