Kucoin / kucoin-python-sdk

MIT License
46 stars 16 forks source link

Websocket handling error and reconnection with TCP_NODELAY=1 #115

Open kheil117 opened 3 months ago

kheil117 commented 3 months ago

Hi team,

When I open a websocket by providing a socket to function KucoinWsClient.create() in order to set TCP_NODELAY to 1 (see example /example_customized_ws_public.py), exceptions are not handled correctly.

When the current websocket connection raises a ConnectionClosedError and then tries to reconnect via _reconnect(), the reconnection fails over and over with error: 'An operation was attempted on something that is not a socket', see below.

Note that without providing a socket to KucoinWsClient.create() and therefore not disabling TCP_NODELAY, the reconnection works.

WARNING _reconnect(112) <Task finished name='Task-X' coro=<ConnectWebsocket._run() done, defined at path\site-packages\kucoin\websocket\websocket.py:34> exception=ConnectionClosedError(None, None, None)> got an exception no close frame received or sent
WARNING _reconnect(124) _reconnect over.
INFO _reconnect(92) Websocket start connect/reconnect
INFO _reconnect(96) asyncio sleep reconnect_wait=1 s reconnect_num=1
INFO _reconnect(98) asyncio sleep ok
INFO _recover_topic_req_msg(127) recover topic event ['/market/level2:HBAR-USDT'] waiting

WARNING _reconnect(112) <Task finished name='Task-XXX' coro=<ConnectWebsocket._run() done, defined at path\site-packages\kucoin\websocket\websocket.py:34> exception=OSError(10038, 'An operation was attempted on something that is not a socket', None, 10038, None)> got an exception [WinError 10038] An operation was attempted on something that is not a socket

WARNING _reconnect(114) pending <Task pending name='Task-XXX' coro=<ConnectWebsocket._recover_topic_req_msg() running at path\site-packages\kucoin\websocket\websocket.py:128> wait_for=<Future pending cb=[Task.__wakeup()]>>
WARNING _reconnect(119) cancel ok.
WARNING _reconnect(124) _reconnect over._