Description
Issue:
If the server sends a valid Close websocket message to the client, the client will throw InvalidOperationException rather than acknowledge/cleanup the connection gracefully.
See relevant point where exception is thrown. here
Close messages from the server can be expected when the server is doing a graceful shutdown, and clients would be expected to reconnect.
Details:
A Close websocket message being received by ClientWebSocket, WebSocketReceiveResult.MessageType will return MessageType.Close. In this case, the message buffer will be zero-bytes, see these remarks.
Currently, Gremlin.NET does not check for this message type in WebSocketConnection.ReceiveMessageAsync and return a zero-length buffer. In Connection.HandleReceiveAsync, the attempt to deserialize the empty buffer returns `null` and the check fails.
Potential Fix
- If `MessageType == Close` Throw a new connection closed exception from WebSocketConnection.ReceiveMessageAsync which includes WebSocketReceiveResult.CloseStatus and WebSocketReceiveResult.CloseDescription in the exception message and as properties.
- This will be caught in Connection.HandleRecieveAsync and treated as fatal and will call CloseConnectionBecauseOfFailureAsync and will handle completing the close handshake, and notifies pending requests on the connection with the closed connection exception.