cryptofeed icon indicating copy to clipboard operation
cryptofeed copied to clipboard

Binance trades feed ERROR code = 1006 (connection closed abnormally

Open cryptorevizor opened this issue 6 years ago • 7 comments

Hi, I fetch trades (by 15 symbols) from binance-futures and store it in postgresql. Time to time I getting this error. Could you please help me to solve this problem?

2020-10-08 19:15:05,532 : WARNING : BINANCE_FUTURES083fca0a-d701-438c-8118-0ade6c36c838: received no messages within timeout, restarting connection
Future exception was never retrieved
future: <Future finished exception=ConnectionClosedError('code = 1006 (connection closed abnormally [internal]), no reason')>
Traceback (most recent call last):
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 827, in transfer_data
    message = await self.read_message()
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 895, in read_message
    frame = await self.read_data_frame(max_size=self.max_size)
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 971, in read_data_frame
    frame = await self.read_frame(max_size)
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 1047, in read_frame
    frame = await Frame.read(
  File "/root/feed/lib/python3.8/site-packages/websockets/framing.py", line 105, in read
    data = await reader(2)
  File "/usr/lib/python3.8/asyncio/streams.py", line 723, in readexactly
    await self._wait_for_data('readexactly')
  File "/usr/lib/python3.8/asyncio/streams.py", line 517, in _wait_for_data
    await self._waiter
asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

websockets.exceptions.ConnectionClosedError: code = 1006 (connection closed abnormally [internal]), no reason
Future exception was never retrieved
future: <Future finished exception=ConnectionClosedError('code = 1006 (connection closed abnormally [internal]), no reason')>
Traceback (most recent call last):
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 827, in transfer_data
    message = await self.read_message()
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 895, in read_message
    frame = await self.read_data_frame(max_size=self.max_size)
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 971, in read_data_frame
    frame = await self.read_frame(max_size)
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 1047, in read_frame
    frame = await Frame.read(
  File "/root/feed/lib/python3.8/site-packages/websockets/framing.py", line 105, in read
    data = await reader(2)
  File "/usr/lib/python3.8/asyncio/streams.py", line 723, in readexactly
    await self._wait_for_data('readexactly')
  File "/usr/lib/python3.8/asyncio/streams.py", line 517, in _wait_for_data
    await self._waiter
asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

websockets.exceptions.ConnectionClosedError: code = 1006 (connection closed abnormally [internal]), no reason
Future exception was never retrieved
future: <Future finished exception=ConnectionClosedError('code = 1006 (connection closed abnormally [internal]), no reason')>
Traceback (most recent call last):
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 827, in transfer_data
    message = await self.read_message()
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 895, in read_message
   frame = await self.read_data_frame(max_size=self.max_size)
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 971, in read_data_frame
    frame = await self.read_frame(max_size)
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 1047, in read_frame
    frame = await Frame.read(
  File "/root/feed/lib/python3.8/site-packages/websockets/framing.py", line 105, in read
    data = await reader(2)
  File "/usr/lib/python3.8/asyncio/streams.py", line 723, in readexactly
    await self._wait_for_data('readexactly')
  File "/usr/lib/python3.8/asyncio/streams.py", line 517, in _wait_for_data
    await self._waiter
asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

websockets.exceptions.ConnectionClosedError: code = 1006 (connection closed abnormally [internal]), no reason
Future exception was never retrieved
future: <Future finished exception=ConnectionClosedError('code = 1006 (connection closed abnormally [internal]), no reason')>
Traceback (most recent call last):
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 827, in transfer_data
    message = await self.read_message()
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 895, in read_message
    frame = await self.read_data_frame(max_size=self.max_size)
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 971, in read_data_frame
    frame = await self.read_frame(max_size)
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 1047, in read_frame
    frame = await Frame.read(
  File "/root/feed/lib/python3.8/site-packages/websockets/framing.py", line 105, in read
    data = await reader(2)
  File "/usr/lib/python3.8/asyncio/streams.py", line 723, in readexactly
    await self._wait_for_data('readexactly')
  File "/usr/lib/python3.8/asyncio/streams.py", line 517, in _wait_for_data
    await self._waiter
asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

websockets.exceptions.ConnectionClosedError: code = 1006 (connection closed abnormally [internal]), no reason
Future exception was never retrieved
future: <Future finished exception=ConnectionClosedError('code = 1006 (connection closed abnormally [internal]), no reason')>
Traceback (most recent call last):
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 827, in transfer_data
    message = await self.read_message()
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 895, in read_message
    frame = await self.read_data_frame(max_size=self.max_size)
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 971, in read_data_frame
    frame = await self.read_frame(max_size)
 File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 1047, in read_frame
    frame = await Frame.read(
  File "/root/feed/lib/python3.8/site-packages/websockets/framing.py", line 105, in read
    data = await reader(2)
  File "/usr/lib/python3.8/asyncio/streams.py", line 723, in readexactly
    await self._wait_for_data('readexactly')
  File "/usr/lib/python3.8/asyncio/streams.py", line 517, in _wait_for_data
    await self._waiter
asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

websockets.exceptions.ConnectionClosedError: code = 1006 (connection closed abnormally [internal]), no reason
2020-10-08 19:15:35,843 : WARNING : BINANCE_FUTURES: encountered connection issue code = 1006 (connection closed abnormally [internal]), no reason - reconnecting...
Traceback (most recent call last):
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 827, in transfer_data
    message = await self.read_message()
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 895, in read_message
    frame = await self.read_data_frame(max_size=self.max_size)
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 971, in read_data_frame
    frame = await self.read_frame(max_size)
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 1047, in read_frame
    frame = await Frame.read(
  File "/root/feed/lib/python3.8/site-packages/websockets/framing.py", line 105, in read
    data = await reader(2)
  File "/usr/lib/python3.8/asyncio/streams.py", line 723, in readexactly
    await self._wait_for_data('readexactly')
  File "/usr/lib/python3.8/asyncio/streams.py", line 517, in _wait_for_data
    await self._waiter
asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/root/feed/lib/python3.8/site-packages/cryptofeed/feedhandler.py", line 222, in _connect
    await self._handler(websocket, feed.message_handler, feed.uuid)
  File "/root/feed/lib/python3.8/site-packages/cryptofeed/feedhandler.py", line 249, in _handler
    async for message in websocket:
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 439, in __aiter__
    yield await self.recv()
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 509, in recv
    await self.ensure_open()
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 812, in ensure_open
    raise self.connection_closed_exc()
websockets.exceptions.ConnectionClosedError: code = 1006 (connection closed abnormally [internal]), no reason
2020-10-13 19:32:00,042 : WARNING : BINANCE_FUTURES: encountered connection issue code = 1006 (connection closed abnormally [internal]), no reason - reconnecting...
Traceback (most recent call last):
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 827, in transfer_data
    message = await self.read_message()
File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 895, in read_message
    frame = await self.read_data_frame(max_size=self.max_size)
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 971, in read_data_frame
    frame = await self.read_frame(max_size)
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 1047, in read_frame
    frame = await Frame.read(
  File "/root/feed/lib/python3.8/site-packages/websockets/framing.py", line 105, in read
    data = await reader(2)
  File "/usr/lib/python3.8/asyncio/streams.py", line 721, in readexactly
    raise exceptions.IncompleteReadError(incomplete, n)
asyncio.exceptions.IncompleteReadError: 0 bytes read on a total of 2 expected bytes

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/root/feed/lib/python3.8/site-packages/cryptofeed/feedhandler.py", line 222, in _connect
    await self._handler(websocket, feed.message_handler, feed.uuid)
  File "/root/feed/lib/python3.8/site-packages/cryptofeed/feedhandler.py", line 249, in _handler
    async for message in websocket:
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 439, in __aiter__
    yield await self.recv()
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 509, in recv
    await self.ensure_open()
  File "/root/feed/lib/python3.8/site-packages/websockets/protocol.py", line 803, in ensure_open
    raise self.connection_closed_exc()
websockets.exceptions.ConnectionClosedError: code = 1006 (connection closed abnormally [internal]), no reason

cryptorevizor avatar Oct 13 '20 20:10 cryptorevizor

see

https://github.com/bmoscon/cryptofeed/blob/master/cryptofeed/feedhandler.py#L128

there is a timeout kwarg that defaults to 120. If you go without an update within that timeout window, the connection will be restarted. There are certainly some symbols and time intervals that might not see trades within that window, so I'd recommend increasing it to whatever you're comfortable with.

bmoscon avatar Oct 13 '20 22:10 bmoscon

Thanks, but I think that it's impossible (on BTC USDT pair binance-futures) that there were't any trade within this big time interval 120 seconds. I think it possible on instrument with poor liquidity. What can I check yet?

cryptorevizor avatar Oct 14 '20 07:10 cryptorevizor

Also, i asked this question in binance-api telegram community and they told me that yesterday was ok, and anyone didn't have problem with websocket stream for futures

cryptorevizor avatar Oct 14 '20 07:10 cryptorevizor

I know of no way to know if the exchange is dropping messages or not unless its something they provide (like with sequence numbers). I dont believe Binance has sequence numbers on trades and I dont think the trade ids are monotonically increasing either. This is why the timeout is there as sort of a failsafe, i think there might be windows where no trades happen, but its hard to know what a good value is for a given exchange, especially one i have almost no experience with. Sounds like you have some experience with it and could probably pick a different timeout for it. You might also look at historical trade data for the past 12 months and see what the longest gap between trades was for your symbols and go with that as a max.

bmoscon avatar Oct 14 '20 12:10 bmoscon

Ohh, maybe I found problem. I ran each symbol separately, 1 symbol = 1 python script, and i think I was block by binance (5 request per seconds(ping-pong)) . Right now, I try to subscribe on all 14 symbols that i need in multiplex stream. So in this case i can't broke binances rate-limit.

cryptorevizor avatar Oct 14 '20 18:10 cryptorevizor

you should be able to subscribe to them all in a single add_feed call

bmoscon avatar Oct 14 '20 18:10 bmoscon

Yes, I did exactly as you said.

Also, if anyone have question about 1006 error this is good explanation: https://github.com/oliver-zehentleitner/unicorn-binance-websocket-api/issues/113

Thank you bmoscon!

cryptorevizor avatar Oct 14 '20 22:10 cryptorevizor