sammchardy / python-binance

Binance Exchange API python implementation for automated trading
https://python-binance.readthedocs.io/en/latest/
MIT License
6.06k stars 2.21k forks source link

Error: CANCEL read_loop after running Async BinanceManager for an hour #1169

Open punmew opened 2 years ago

punmew commented 2 years ago

Describe the bug System crash after running BinanceManager for an hour

To Reproduce import asyncio from binance import AsyncClient, BinanceSocketManager

async def main(): client = await AsyncClient.create() bm = BinanceSocketManager(client)

start any sockets here, i.e a trade socket

ts = bm.trade_socket('BNBBTC')
# then start receiving messages
async with ts as tscm:
    while True:
        res = await tscm.recv()
        print(res)

await client.close_connection()

if name == "main":

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Expected behavior BinanceManager stream the ticker data without error

Environment (please complete the following information): Python version: 3.9.7 Virtual Env: conda OS: Windows11 python-binance version: 1.0.15

Logs or Additional context CANCEL read_loop Traceback (most recent call last): File "d:\OneDrive\OIG Tech\Tradingview_BOT\binancesocket.py", line 202, in

File "C:\ProgramData\Anaconda3\lib\asyncio\base_events.py", line 642, in run_until_complete return future.result() File "d:\OneDrive\OIG Tech\Tradingview_BOT\binancesocket.py", line 179, in main async with ts as tscm: KeyError: 'data' Unclosed client session client_session: <aiohttp.client.ClientSession object at 0x000001D93DCF9D30>

LeviathanLevi commented 2 years ago

Same issue here, after about 2 - 8 hours I get an error saying CANCEL read_loop stream. Do we need to resubscribe to the websocket stream after awhile?

DimPap99 commented 2 years ago

I have the same issue. I read somewhere that the problem lies in the websocket version. In websocket 10+ the problem is fixed but python-binance 1.0.15 requires and older version. Hopefully they will fix it soon.

myown-del commented 2 years ago

Same issue

jhogg11 commented 2 years ago

I have the same issue. I read somewhere that the problem lies in the websocket version. In websocket 10+ the problem is fixed but python-binance 1.0.15 requires and older version. Hopefully they will fix it soon.

I have a VPS where I run a bot using this package and I checked the websockets version. It's running 9.1, so I downgraded to that version and I still get the error.

Karlheinzniebuhr commented 2 years ago

Same issue ERROR:binance.streams:CANCEL read_loop

d3thomas commented 2 years ago

Same issue here. My terminal is full with CANCEL read_loop

Could somebody please explain why this could happen and how we can fix this?

fasciinatiid commented 2 years ago

I experienced the same thing building a multiplex socket. Oddly, before I converted it from a symbol_ticker over to the multiplex, I didn't have the issue, but once I did change it, that's when it started appearing. Now, I wouldn't say this is the most elegant of solutions, but the way I stopped it is I went to the module file C:\Program Files\Python39\Lib\site-packages\binance\streams.py, and found the line: 'self._log.error("CANCEL read_loop")', and I commented it out. It was printing it because of an error, but the error doesn't mean it's so broken that my program couldn't run, so I won't use the logging on it.

iliyann commented 2 years ago

I see the same issue with multiplex socket. Didn't have that with individual aggtrade_socket.

dlefcoe commented 1 year ago

when you say multiplex socket - is that because for example you run code from, say a python file and then other code on the same account from a notebook (*.ipynb) therefore opening two sockets ?

fasciinatiid commented 1 year ago

when you say multiplex socket - is that because for example you run code from, say a python file and then other code on the same account from a notebook (*.ipynb) therefore opening two sockets ?

Good question, but no. Multiplex is the solution for having multiple ticker streams in a single socket. I was concerned about issues with multiple sockets being opened simultaneously, so just to cross my j's, I cleared all of my API keys and created a new one just for it, but to no avail. However, the question makes me wonder if the multiplex socket itself could be causing a multi-socket error

dlefcoe commented 1 year ago

interesting. at this point in time I just have one ticker stream and the only thing that i was doing was working in a notebook on the side (so one python file connecting to trade and the notebook for analysing the trade stats manually). So i would have been connecting in the same way as the python file... i have had this error message a few times and need to eliminate or handle it.

dlefcoe commented 1 year ago

I experienced the same thing building a multiplex socket. Oddly, before I converted it from a symbol_ticker over to the multiplex, I didn't have the issue, but once I did change it, that's when it started appearing. Now, I wouldn't say this is the most elegant of solutions, but the way I stopped it is I went to the module file C:\Program Files\Python39\Lib\site-packages\binance\streams.py, and found the line: 'self._log.error("CANCEL read_loop")', and I commented it out. It was printing it because of an error, but the error doesn't mean it's so broken that my program couldn't run, so I won't use the logging on it.

do you cancel just this line of also the await self.kill_read_loop() in the line below (ie. the whole if statement) ?

AndreyAAleksandrov commented 1 year ago

In my situation this problem were happen when callback method include calculation logic and takes a lot of time.

Schema: -> twm callback method and wait for feedback of finishing method -> callback method self.handle_orderbook for calculations takes > twm timeout -> twm CANCEL read_loop generated

Solved by using Process and Queue:

class Binance_Realtime: def init(self, coin_set): self.coin_set = coin_set self.parameters = Binance_Parameters() self.twm = ThreadedWebsocketManager(api_key=self.parameters.api_key, api_secret=self.parameters.api_secret)

def run(self, queue_server, queue_client): self.queue_client = queue_client self.twm.start() for coin in self.coin_set: self.twm.start_depth_socket(callback=self.handle_orderbook, symbol=coin) self.twm.start_kline_socket(callback=self.handle_kline, symbol=coin) self.twm.join()

def handle_orderbook(self, orderchange): self.queue_client.put(orderchange)

def handle_kline(self, kline): self.queue_client.put(kline)