From ff6cd140cde5d1d519446c0a478e04f58b9a97b2 Mon Sep 17 00:00:00 2001 From: Thom de Jong Date: Mon, 30 Sep 2019 15:22:09 +0200 Subject: [PATCH] Only close destination when websocket is still open or capable of handling the close message --- .../Middleware/WebSocketsProxyMiddleware.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Ocelot/WebSockets/Middleware/WebSocketsProxyMiddleware.cs b/src/Ocelot/WebSockets/Middleware/WebSocketsProxyMiddleware.cs index 6d56a4899..b35a1894e 100644 --- a/src/Ocelot/WebSockets/Middleware/WebSocketsProxyMiddleware.cs +++ b/src/Ocelot/WebSockets/Middleware/WebSocketsProxyMiddleware.cs @@ -44,9 +44,24 @@ private static async Task PumpWebSocket(WebSocket source, WebSocket destination, } catch (OperationCanceledException) { - await destination.CloseOutputAsync(WebSocketCloseStatus.EndpointUnavailable, null, cancellationToken); + if (destination.State == WebSocketState.Open || destination.State == WebSocketState.CloseReceived) + { + await destination.CloseOutputAsync(WebSocketCloseStatus.EndpointUnavailable, null, cancellationToken); + } return; } + catch (WebSocketException e) + { + if (e.WebSocketErrorCode == WebSocketError.ConnectionClosedPrematurely) + { + if (destination.State == WebSocketState.Open || destination.State == WebSocketState.CloseReceived) + { + await destination.CloseOutputAsync(WebSocketCloseStatus.EndpointUnavailable, null, cancellationToken); + } + return; + } + throw; + } if (result.MessageType == WebSocketMessageType.Close) {