doghappy / socket.io-client-csharp

socket.io-client implemention for .NET
MIT License
729 stars 125 forks source link

Payload from multiple emitAsyncs getting grouped together in the listener #234

Closed cakesoft-raghav closed 2 years ago

cakesoft-raghav commented 2 years ago

On my nodejs socketio server, I am sometimes seeing that the listener is trying to parse payload from multiple emits. The raw string that the listener receives in these cases looks something like this

2["/syncStatus",{
    "prop1": "abcd",
    "list1": [
        {
            "innerprop1":"1234\n",
            "innerprop2":"5678\n"
        },
        {
            "innerprop1":"9101\n",
            "innerprop2":"1213\n2["syncStatus",{"prop1": "xyz","list1":...etc}]

You can see how some other payload has interrupted the current one. This ends up leading to JSON parsing errors on the server side. On the C# client side, I am emitting from async event handlers. The events fire after completion of specific tasks, data from which is then sent to the server.

doghappy commented 2 years ago

With this information, I cannot determine the real cause.

Can you create a reproducible repo? Thanks ❤️

cakesoft-raghav commented 2 years ago

Sure! I'll update you in few hours with the repo

cakesoft-raghav commented 2 years ago

Hey sorry for the delay but I have created a repo to reproduce this error and also included steps to do that. Here it is

doghappy commented 2 years ago

@cakesoft-raghav Hi, Thanks for your repo, but I can't reproduce it, it seems to work, here is the log:

Hello World!
Socket_OnConnected
Socket.Id:3xW9sVFLYrZDTAdjAAAB
Doing task
Sending data id: 0, data-len: 99000
Sending data id: 1, data-len: 99000
Sending data id: 2, data-len: 99000
Sending data id: 3, data-len: 99000
Sending data id: 4, data-len: 99000
Sending data id: 5, data-len: 99000
Sending data id: 6, data-len: 99000
Sending data id: 7, data-len: 99000
Sending data id: 8, data-len: 99000
Sending data id: 9, data-len: 99000
Sending data id: 10, data-len: 99000
Sending data id: 11, data-len: 99000
Sending data id: 12, data-len: 99000
Sending data id: 13, data-len: 99000
Sending data id: 14, data-len: 99000
Sending data id: 15, data-len: 99000
Sending data id: 16, data-len: 99000
Sending data id: 17, data-len: 99000
Sending data id: 18, data-len: 99000
Sending data id: 19, data-len: 99000

PS D:\repo\SocketIOErrorTest\socketIOServer> npm start

> socketioserver1@0.0.0 start D:\repo\SocketIOErrorTest\socketIOServer
> node app.js

listening on *:8089
Socket 3xW9sVFLYrZDTAdjAAAB connected
Response received
Response: 0, response-length: 99000

Response received
Response: 1, response-length: 99000

Response received
Response: 2, response-length: 99000

Response received
Response: 3, response-length: 99000

Response received
Response: 4, response-length: 99000

Response received
Response: 5, response-length: 99000

Response received
Response: 6, response-length: 99000

Response received
Response: 7, response-length: 99000

Response received
Response: 8, response-length: 99000

Response received
Response: 9, response-length: 99000

Response received
Response: 10, response-length: 99000

Response received
Response: 11, response-length: 99000

Response received
Response: 12, response-length: 99000

Response received
Response: 13, response-length: 99000

Response received
Response: 14, response-length: 99000

Response received
Response: 15, response-length: 99000

Response received
Response: 16, response-length: 99000

Response received
Response: 17, response-length: 99000

Response received
Response: 18, response-length: 99000

Response received
Response: 19, response-length: 99000

Find the function tryParse, replace it's content with return JSON.parse(str) to see the actual JSON parsing error

I tried, but no error occurred, so there is no exception log. Other than that, I did not make any changes.

OS: Windows 11 21H2 22000.282 nodejs: v12.18.4

Some relevant information in package-lock.json

    "socket.io": {
      "version": "4.3.1",
      "resolved": "https://registry.npmjs.org/socket.io/-/socket.io-4.3.1.tgz",
      "integrity": "sha512-HC5w5Olv2XZ0XJ4gOLGzzHEuOCfj3G0SmoW3jLHYYh34EVsIr3EkW9h6kgfW+K3TFEcmYy8JcPWe//KUkBp5jA==",
      "dev": true,
      "requires": {
        "accepts": "~1.3.4",
        "base64id": "~2.0.0",
        "debug": "~4.3.2",
        "engine.io": "~6.0.0",
        "socket.io-adapter": "~2.3.2",
        "socket.io-parser": "~4.0.4"
      },
cakesoft-raghav commented 2 years ago

Can you try increasing the data length ? In SocketIOClientApp/DummyTask in ReadFile function I have substringed the length to 99000. For me, I don't get the error for strings lesser than that length. Maybe you'll get it for a larger one. For me, at ~million characters, the server kicks the client without any error

doghappy commented 2 years ago

I increased the length from 99 000 to 920 000 and it still works. When I increase it to 930 000, the server will disconnect, the reason is: Close Reason: MessageTooBig, no error is thrown

...

Response received
Response: 16, response-length: 920000

Response received
Response: 17, response-length: 920000

Response received
Response: 18, response-length: 920000

Response received
Response: 19, response-length: 920000
cakesoft-raghav commented 2 years ago

Yeah it seems the error is dependent on some more variables than just string length. I'll do some testing and get back to you.

cakesoft-raghav commented 2 years ago

Can you try running the client and server on different devices ? I've updated the repo to let you input an ip address before connecting.

doghappy commented 2 years ago

Can you try running the client and server on different devices ?

Yes, I successfully reproduced

doghappy commented 2 years ago

I released a new version, trying to fix it.

3.0.4-alpha.1

cakesoft-raghav commented 2 years ago

Hey thanks a lot for the quick update. Seems to be working great now