microsoft / microsoft/vs-streamjsonrpc

WebSocket gracefull shutdown exception

Open
#604 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
937
Forks
178
Avg merge
3d 1h
Merged PRs (30d)
28

Description

Hi,
I have regular .net clients and aspNetCore server. When client closes I call webSocket.CloseOutputAsync. This works, I get no exceptions on client or server.

But when trying to do the same on server, it always generate exception, and I tried both webSocket.CloseAsync or webSocket.CloseOutputAsync. With webSocket.CloseOutputAsync I get an exeption on server happens here in WebSocketMessageHandler.cs:

      if (result.MessageType == WebSocketMessageType.Close)
                    {
                        await this.WebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closed as requested.", CancellationToken.None).ConfigureAwait(false);
                        return null;
                    }

Exception thrown: 'System.Net.WebSockets.WebSocketException' in System.Private.CoreLib.dll
The WebSocket is in an invalid state ('Closed') for this operation. Valid states are: 'Open, CloseReceived, CloseSent'

I changed the code to this and it seems to fix the exception, but not sure if its the correct fix:

if (result.MessageType == WebSocketMessageType.Close)
                    {
                        if (this.WebSocket.State == WebSocketState.CloseReceived)
                            await this.WebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closed as requested.", CancellationToken.None).ConfigureAwait(false);
                        return null;
                    }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in WebSocketMessageHandler.cs at the WebSocketMessageType.Close branch and trace the WebSocket state before CloseAsync or CloseOutputAsync is called. Reproduce the server-side close with the described .NET client and verify that graceful shutdown does not attempt an invalid operation or raise WebSocketException.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.