ccxt: [binance] future websocket error: connection closed by remote server

  • OS: centos7
  • Programming Language version: NodeJS
  • CCXT version: ccxt.pro@0.3.55

We have been trading binance future with ccxt.pro and it has been working fine most of the times. However, every once in a while we receive the following errors:

  • connection closed by remote server, closing code 1006
  • Connection to wss://fstream.binance.com/ws timed out due to a ping-pong keepalive missing on time.

We suspect the code that cause the problem is the watchOrderBook method, but not 100% sure. Here is the full log for the first error.

 NetworkError: connection closed by remote server, closing code 1006
    at WsClient.onClose (/myProject/node_modules/ccxt.pro/js/base/Client.js:225:25)
    at WebSocket.onClose (/myProject/node_modules/ccxt.pro/node_modules/ws/lib/event-target.js:129:16)
    at WebSocket.emit (events.js:311:20)
    at WebSocket.EventEmitter.emit (domain.js:482:12)
    at WebSocket.emitClose (/myProject/node_modules/ccxt.pro/node_modules/ws/lib/websocket.js:191:10)
    at TLSSocket.socketOnClose (/myProject/node_modules/ccxt.pro/node_modules/ws/lib/websocket.js:858:15)
    at TLSSocket.emit (events.js:323:22)
    at TLSSocket.EventEmitter.emit (domain.js:482:12)
    at net.js:668:12           
    at TCP.done (_tls_wrap.js:556:7) {
  constructor: [Function: NetworkError],
  name: 'NetworkError'
}

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 24 (5 by maintainers)

Most upvoted comments

The latest ccxt is still experiencing the same issue , although the bug fix suggested by @FakePsyho is already added to cache.py. Is there any updates on this issue? Thanks!

@FakePsyho thanks for finding this bug, I’ve used your code to fix it just now

@kroitor I figured that it’s easier for me to find the bug than to file a proper report. Since I can’t reference the code (different repo) I’ll explain the problem.

In cache.py ArrayCacheBySymbolById.append() has a bug. by_id points to self.hashmap[item['symbol']]. When the data structure is full and you’re removing an element, you’re trying to remove it from the ‘by_id’, but this will fail if removed element uses different symbol than the one just added.

This:

if len(self._deque) == self._deque.maxlen:
            self._deque.popleft()
            delete_reference = self._index.popleft()
            del by_id[delete_reference]

Could be this:

if len(self._deque) == self._deque.maxlen:
            delete_item = self._deque.popleft()
            delete_reference = self._index.popleft()
            del self.hashmap[delete_item['symbol']][delete_reference]

I’m surprised that this bug exist, as I’d imagine this is exchange-agnostic and this bug probably crashes every exchange after placing ordersLimit orders when using watch* method that populates self.orders

Noting that we too have had the second error on Binance spot occasionally. Connection to wss://stream.binance.com:9443/ws/... timed out due to a ping-pong keepalive missing on time. Perhaps the ping-pong is not implemented according to what Binance wants, or there is a problem on the server side. Not sure if it’s related or will help, but this error also tends to pop up around when the ping-pong error does: Exception in callback FastClient.receive_loop.<locals>.handler() at ...\Python\Python38\site-packages\ccxtpro\base\fast_client.py:19 handle: <Handle FastClient.receive_loop.<locals>.handler() at ...\Python\Python38\site-packages\ccxtpro\base\fast_client.py:19> Traceback (most recent call last): File "C:\Program Files (x86)\Python38-32\lib\asyncio\events.py", line 81, in _run self._context.run(self._callback, *self._args) File "...\Python\Python38\site-packages\ccxtpro\base\fast_client.py", line 23, in handler self.handle_message(message) File "...\Python\Python38\site-packages\ccxtpro\base\aiohttp_client.py", line 32, in handle_message self.handle_text_or_binary_message(message.data) File "...\Python\Python38\site-packages\ccxtpro\base\aiohttp_client.py", line 27, in handle_text_or_binary_message self.on_message_callback(self, decoded) File "...\Python\Python38\site-packages\ccxtpro\binance.py", line 739, in handle_message return method(client, message) File "...\Python\Python38\site-packages\ccxtpro\binance.py", line 718, in handle_order orders.append(parsed) File "...\Python\Python38\site-packages\ccxtpro\base\cache.py", line 75, in append del by_id[delete_reference['id']] KeyError: '2026130'

Because you have to do exchange.close() once every day or hour, you can’t just let watch_trades() run indefinitely. You can find my repo “b-arbitrage-system”, in my async loop (on bot-fake-money.py) I put a code to reload the exchange after some time. Le ven. 27 janv. 2023 à 17:17, Kristof Jozsa @.> a écrit : I’m also experiencing this behavior when watching trades (and that only) on websocket using ccxt. Any hints how I can improve my code to get rid ot this? — Reply to this email directly, view it on GitHub <#7858 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AU4OAF7A7XHZJEKTHR3DOM3WUPYK3ANCNFSM4S6A4MVQ . You are receiving this because you commented.Message ID: @.>

@nelso0 can you share code to reload exchange periodically, m also trying to fetch watch_ticker of currency but some time it produces error 1006, thanks …

Im still experience this issue even though i use exchange.close() anyone solved this?