Exception handling in DefaultWebSocketHandler

Found in

EPiServer.CMS.Core 11.15.1

Fixed in

EPiServer.CMS.UI 11.29.0

Created

Aug 04, 2020

Updated

Sep 15, 2020

Area

CMS UI

State

Closed, Fixed and tested


Description

The problem is that an error closing the socket will not be caught, and instead cause an Unhandled Exception log, unnecessarily and confusingly.

We are seeing quite few event logs like this (WebSockets works fine, it's not a problem with the connection as such):

Exception message: The WebSocket instance cannot be used for communication because it has been transitioned into an invalid state.
at System.Web.WebSockets.AspNetWebSocket.ThrowIfAborted()
at System.Web.WebSockets.AspNetWebSocket.CloseAsyncImpl(WebSocketCloseStatus closeStatus, String statusDescription, CancellationToken cancellationToken, Boolean performValidation)
at System.Web.WebSockets.AspNetWebSocket.CloseAsync(WebSocketCloseStatus closeStatus, String statusDescription, CancellationToken cancellationToken)
at EPiServer.Shell.UI.Messaging.Internal.DefaultWebSocketHandler.<ProcessWebSocketRequestAsync>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at EPiServer.Shell.UI.Messaging.Internal.PushMessageHandler.<ProcessWebSocketRequest>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.WebSocketPipeline.<ProcessRequestImplAsync>d__9.MoveNext()

The crucial call is:

System.Web.WebSockets.AspNetWebSocket.CloseAsync() in DefaultWebSocketHandler.ProcessWebSocketRequestAsync()

The problem is that an error closing the socket will not be caught, and instead cause an Unhandled Exception log, unnecessarily and confusingly.

This is not reasonable, since an error closing it is more or less to be expected.