‘Invalid UTF-8 code encountered.’ close reason with QWebSocket

I get close reason: ‘Invalid UTF-8 code encountered.’ (code=1007) with QWebSocket in the following scenario:

  • I connect to Binance stream wss://stream.binance.com:9443/stream with QWebSocket::open.
  • Successfully receive messages for some time period (a few hours).
  • Binance disconnects me when its servers load increases (because I have low-priority API) and I get close reason code=1000 that indicates a normal closure.
  • Try to reconnect multiple times by calling QWebSocket::open again, but Binance closes the connection with close reason ‘Invalid UTF-8 code encountered.’ (code=1007) that indicates that an endpoint is terminating the connection because it has received data within a message that was not consistent with the type of the message (e.g., non-UTF-8 [RFC3629] data within a text message).
  • Restart the application manually and connect successfully.

The difficulty of reproducing the bug is that Binance disconnects me rarely (once a day, for example) and experimentation with disconnecting the network cable from my machine does not have this effect, after I connect the cable back, QWebSocker reconnects successfully. When I disconnect the network cable I get Error: ‘Unknown error’ (code=-1), Close reason: ” (code=1000).

So, one of the possible assumptions is that something goes wrong with QWebSocket after Binance disconnects it with code 1000.

I tried to recreate QWebSocket object before reconnecting as a workaround and probably it helped:

Log messages with debug level:

My environment: Windows 10, MSVC 2019, QT 5.15.2

Noticed it disconnected with code 1008:

Leave a Reply

Your email address will not be published. Required fields are marked *