Client Connection closes after ~60 seconds

Open
#43 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
csharp
Domain
networking

Research direction

Reproduce the client and server setup shown, using WebSocket.Connect and AddWebSocketService, while observing the roughly 60-second disconnect. Start with the WsFrame.ParseAsync and Ext.ReadBytesAsync stack-trace paths, then trace the close through Service.cs:56 and Service.cs:61. Done means the cause is identified and the connection no longer closes unexpectedly at that interval.

Written by the indexing model from the issue text.

Description

Good Morning STA!

I'm getting the following exception after approx 60 seconds of my client running:

09.05.2014 15:08:28|Fatal|WebSocket.acceptException|WebSocketSharp.WebSocketException: The header part of a frame cannot be read from the data source.
    at WebSocketSharp.WsFrame.<>c__DisplayClasse.<ParseAsync>b__d(Byte[] header)
    at WebSocketSharp.Ext.<>c__DisplayClass6.<ReadBytesAsync>b__5(IAsyncResult ar)

09.05.2014 15:08:28|Fatal|WebSocket.acceptException|System.ObjectDisposedException: Cannot access a disposed object.
    Object name: 'System.Net.Sockets.NetworkStream'.
        at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
        at WebSocketSharp.Ext.<>c__DisplayClass6.<ReadBytesAsync>b__5(IAsyncResult ar)

Service.cs:56   [ERROR]         Client: An exception has occurred while receiving a message.

--------------------------------------------------------------------------------------

09.05.2014 15:08:42|Fatal|WebSocket.acceptException|System.ObjectDisposedException: Cannot access a disposed object.
    Object name: 'System.Net.Sockets.NetworkStream'.
    at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
    at WebSocketSharp.Ext.<>c__DisplayClass6.<ReadBytes

    Async>b__5(IAsyncResult ar)
    09.05.2014 15:08:42|Fatal|WebSocket.acceptException|WebSocketSharp.WebSocketException: The header part of a frame cannot be read from the data source.
    at WebSocketSharp.WsFrame.<>c__DisplayClasse.<ParseAsync>b__d(Byte[] header)
    at WebSocketSharp.Ext.<>c__DisplayClass6.<ReadBytesAsync>b__5(IAsyncResult ar)






GameEngine.cs:372       [GAME]          per used Per
Service.cs:61   [GAME]          Client: 127.0.0.1:1542 disconnected. Reason:
Client: 127.0.0.1:1542 disconnected. Reason: 09.05.2014 15:18:23|Fatal|WebSocket.acceptException|System.ObjectDisposedException: Cannot access a disposed object.

Object name: 'System.Net.Sockets.NetworkStream'.
at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
at WebSocketSharp.Ext.<>c__DisplayClass6.<ReadBytesAsync>b__5(IAsyncResult ar)

Service.cs:56   [ERROR]         Client: An exception has occurred while receiving a message.
09.05.2014 15:18:24|Fatal|WebSocket.acceptException|System.IO.IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine. ---> System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine
at System.Net.Sockets.Socket.BeginReceive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, AsyncCallback callback,Object state)
at System.Net.Sockets.NetworkStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)

--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
at WebSocketSharp.Ext.ReadBytesAsync(Stream stream,Int32 length, Action`1 completed, Action`1 error)
at WebSocketSharp.WsFrame.ParseAsync(Stream stream,Boolean unmask, Action`1 completed, Action`1 error)
at WebSocketSharp.WsStream.ReadFrameAsync(Action`1completed, Action`1 error)
at WebSocketSharp.WebSocket.<>c__DisplayClass18.<startReceiving>b__15()
at WebSocketSharp.WebSocket.<>c__DisplayClass18.<startReceiving>b__16(WsFrame frame)
at WebSocketSharp.WsFrame.<>c__DisplayClasse.<ParseAsync>b__d(Byte[] header)
at WebSocketSharp.Ext.<>c__DisplayClass6.<ReadBytesAsync>b__5(IAsyncResult ar)
Service.cs:56   [ERROR]         Client: The WebSocket connection has already been closed.

I'm connecting with the collowing code

            Stopwatch watch = new Stopwatch();
            watch.Start();

            ws = new WebSocket("ws://localhost:8140/game");


            ws.OnMessage += (sender, e) =>
                this.Progress(e);

            ws.OnError += (sender, e) =>
                Console.WriteLine("Stopwatch: " + watch.ElapsedMilliseconds);

            ws.OnClose += (sender, e) =>
                Console.WriteLine("Stopwatch: " + watch.ElapsedMilliseconds);

            ws.Connect();
            this.Login(hash);

And my server looks like this:

            GameEngine gameEngine = new GameEngine();
            ChatEngine chatEngine = new ChatEngine();


            wssv.AddWebSocketService<GameService>("/game", () => new GameService(gameEngine));
            wssv.AddWebSocketService<ChatService>("/chat", () => new ChatService(chatEngine));
            wssv.AddWebSocketService<AdminService>("/admin", () => new AdminService(gameEngine, chatEngine));
            wssv.Log.Level = LogLevel.Fatal;

            wssv.Start();

            Console.ReadKey(true);
            wssv.Stop();

I logged duration of the connection three times, and i got the following timings:
57835ms First run
56207ms Second run
57343ms Third run

I'm not sure if this is something with websocket-sharp, but maybe you know the problem? 👍

Dominant language
C#
Stars
6.1k
Forks
1.7k
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

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.

More from sta/websocket-sharp

All issues in sta/websocket-sharp

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.