ArduinoWebsockets icon indicating copy to clipboard operation
ArduinoWebsockets copied to clipboard

Random disconnects, fails on binary ping including 0x00

Open git-eri opened this issue 2 years ago • 2 comments

I experienced random disconnects from my ESP8266 Websocket Client.

The websocket server is uvicorn 0.15.0 and i compared the debug logs from uvicorn with the crashes from the ESP. Then i saw a pattern: Everytime a binary ping from uvicorn that is including 0x00 the ESP responds with a wrong, cut-off pong.

I took three samples:

Error 1:
uvicorn.error - DEBUG - % sending keepalive ping
uvicorn.error - DEBUG - > PING 71 00 01 c6 [binary, 4 bytes]
uvicorn.error - DEBUG - < PONG 'q' [text, 1 byte]
uvicorn.error - DEBUG - ! timed out waiting for keepalive pong

Error 2:
uvicorn.error - DEBUG - % sending keepalive ping
uvicorn.error - DEBUG - > PING 06 00 5c 93 [binary, 4 bytes]
uvicorn.error - DEBUG - < PONG '\x06' [text, 1 byte]
uvicorn.error - DEBUG - ! timed out waiting for keepalive pong

Error 3:
uvicorn.error - DEBUG - % sending keepalive ping
uvicorn.error - DEBUG - > PING a4 5e b3 00 [binary, 4 bytes]
uvicorn.error - DEBUG - < PONG a4 5e b3 [binary, 3 bytes]
uvicorn.error - DEBUG - ! timed out waiting for keepalive pong

The library seems to cut off before 0x00 and then sends it. 0x71 = "q" as text. I don't know why the first one is interpreted as text and the other as binary but the other ones are obvious.

I know this library isn't that active but this may save someone else from the headache that i had from figuring this out as some time passed on waiting for uvicorn to send the right ping packet and narrowing this down.

Or maybe someone could help with fixing this bug or else i have to search for an alternative.

git-eri avatar Feb 04 '24 21:02 git-eri

Hi, I faced the same problem and finally found a solution. 0x00 bytes were lost in functions

WSString fromInterfaceString WSInterfaceString fromInternalString

The solution is to modify the argument passed to pong in file websockets_endpoint.cpp. It should be pong(std::move(msg.rawData())), when was this pong(internals::fromInterfaceString(msg.data())); then the data passed through the c_str() function twice, which caused String to be read only until 0x00 was encountered.

abukkawon avatar Mar 15 '24 13:03 abukkawon