bybit-exchange / pybit

Official Python3 API connector for Bybit's HTTP and WebSockets APIs.
Other
418 stars 135 forks source link

High CPU load on websocket error #201

Closed roberto-paes closed 6 months ago

roberto-paes commented 7 months ago

When an error occurs in the websocket connection, the process starts using 100% of CPU, affecting the entire environment. In the example below, the problem occurred after a timeout. I'm using the latest version of pybit.

Mar 29 13:40:38  WebSocket Unified V5 (wss://stream.bybit.com/v5/public/linear) encountered error: ping/pong timed out.
Mar 29 13:40:47  2024-03-29 13:40:46 - pybit._http_manager - ERROR - invalid request, please check your server timestamp or recv_window param. req_timestamp[1711719639084],server_timestamp[1711719644391],recv_window[5000] (ErrCode: 10002). Added 2.5 seconds to recv_window. 2 retries remain.
Mar 29 13:41:14  2024-03-29 13:41:14 - pybit._http_manager - ERROR - invalid request, please check your server timestamp or recv_window param. req_timestamp[1711719651315],server_timestamp[1711719673618],recv_window[7500] (ErrCode: 10002). Added 2.5 seconds to recv_window. 1 retries remain.
Mar 29 13:41:28  2024-03-29 13:41:28 - pybit._http_manager - ERROR - invalid request, please check your server timestamp or recv_window param. req_timestamp[1711719677424],server_timestamp[1711719687876],recv_window[10000] (ErrCode: 10002). Added 2.5 seconds to recv_window. 0 retries remain.
Mar 29 13:42:05  WebSocket Unified V5 (wss://stream.bybit.com/v5/public/linear) encountered error: ping/pong timed out.
Mar 29 13:42:47  WebSocket Unified V5 (Auth) (wss://stream.bybit.com/v5/private) encountered error: ping/pong timed out.
Kamsby commented 7 months ago

Mar 29 13:41:28 2024-03-29 13:41:28 - pybit._http_manager - ERROR - invalid request, please check your server timestamp or recv_window param. req_timestamp[1711719677424],server_timestamp[1711719687876],recv_window[10000] (ErrCode: 10002). Added 2.5 seconds to recv_window. 0 retries remain.

Sync your system/server time and try again ( set time automatically from Date&Time)

roberto-paes commented 7 months ago

Thank You, Kamsby. The log about time setting appears to be the consequence rather than the cause. The AWS server runs out of available processing capacity to send messages within the expected time window. Ping timeouts generally occur in times of market stress and something in the error handling seems to be leading to excessive CPU consumption.

dextertd commented 7 months ago

pybit uses a while loop to reconnect and I think that this generally occupies a CPU core. I'm not sure if there is a way around this. It certainly shouldn't occupy the entirety of a CPU.

roberto-paes commented 6 months ago

Hi. Just to let you know, since I used a time.sleep(1) inside the while loop, the problem went away. I still see disconnections from bybit, but the program has CPU available to reconfigure itself.

dextertd commented 6 months ago

Using time instead of passing works well. Will keep pass in the doc as it makes the example code simpler though.