python-binance icon indicating copy to clipboard operation
python-binance copied to clipboard

Mysteries "Remote end closed connection without response" - How to deal with it?

Open trueToastedCode opened this issue 1 year ago • 5 comments

Sometimes my bot fails simply fetching balance details although the internet connection is fine.

import os
from pathlib import Path
from binance.client import Client

client = Client(
    api_key='<your api key>',
    private_key=Path(
        os.path.dirname(os.path.realpath(__file__)),
        'your-prv-key.pem'
    )
)

# possibly wait for hours or days

client.get_asset_balance(asset='USDT')['free']
Traceback (most recent call last):
  File "/app/main.py", line 25, in on_candles
    strategy.on_candles(*args, **kwargs)
  File "/app/strategy/strategy.py", line 365, in on_candles
    quote_quantity = self.__broker.get_free_asset_balance(self.__quote_asset)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/strategy/binance_broker.py", line 158, in get_free_asset_balance
    return Decimal(self.__client.get_asset_balance(asset=asset)['free'])
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/binance/client.py", line 2295, in get_asset_balance
    res = self.get_account(**params)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/binance/client.py", line 2272, in get_account
    return self._get("account", True, data=params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/binance/client.py", line 531, in _get
    return self._request_api("get", path, signed, version, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/binance/client.py", line 475, in _request_api
    return self._request(method, uri, signed, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/binance/client.py", line 450, in _request
    self.response = getattr(self.session, method)(uri, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/requests/sessions.py", line 602, in get
    return self.request("GET", url, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/requests/sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/requests/sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/requests/adapters.py", line 682, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

This is bad, cz some strategies calculate signals rather rare and if it then can't open a position, it impacts overall performance. What's the best way to deal with it?

trueToastedCode avatar Nov 26 '24 15:11 trueToastedCode

hi @trueToastedCode , have you been able to find a way to reproduce it consitently? Would it work for your case if you put it in a try except clause, and retry on error?

pcriadoperez avatar Nov 27 '24 00:11 pcriadoperez

hi @trueToastedCode , have you been able to find a way to reproduce it consitently? Would it work for your case if you put it in a try except clause, and retry on error?

Yes, it turned out that this actually happens every single time in my strategy... it holds a client instance which after some time just doesn't work anymore. Maybe as some sideffect? My strategy also holds a second client Instance (in a library) without credentials which is just needed to fetch some history as well as a WebSocket connection.

Simply re-instantiating the client when needed seemed to fix the issue for now.

trueToastedCode avatar Dec 13 '24 13:12 trueToastedCode

hi @trueToastedCode , have you been able to find a way to reproduce it consitently? Would it work for your case if you put it in a try except clause, and retry on error?

Yes, it turned out that this actually happens every single time in my strategy... it holds a client instance which after some time just doesn't work anymore. Maybe as some sideffect? My strategy also holds a second client Instance (in a library) without credentials which is just needed to fetch some history as well as a WebSocket connection.

Simply re-instantiating the client when needed seemed to fix the issue for now.

Can you please shed more details on how you fixed it ? It seems I am having the same issue.

mndiarra avatar Mar 08 '25 21:03 mndiarra

hi @trueToastedCode , have you been able to find a way to reproduce it consitently? Would it work for your case if you put it in a try except clause, and retry on error?

Yes, it turned out that this actually happens every single time in my strategy... it holds a client instance which after some time just doesn't work anymore. Maybe as some sideffect? My strategy also holds a second client Instance (in a library) without credentials which is just needed to fetch some history as well as a WebSocket connection. Simply re-instantiating the client when needed seemed to fix the issue for now.

Can you please shed more details on how you fixed it ? It seems I am having the same issue.

Test the client e.g. by fetching the free asset balance. If you get an error, first make sure the api is reachable and then make a new client instance. This is how I worked around this issue which occurs for me till this day with the latest python-binance version.

trueToastedCode avatar Mar 26 '25 14:03 trueToastedCode

hi @trueToastedCode , have you been able to find a way to reproduce it consitently? Would it work for your case if you put it in a try except clause, and retry on error?

Yes, it turned out that this actually happens every single time in my strategy... it holds a client instance which after some time just doesn't work anymore. Maybe as some sideffect? My strategy also holds a second client Instance (in a library) without credentials which is just needed to fetch some history as well as a WebSocket connection. Simply re-instantiating the client when needed seemed to fix the issue for now.

Can you please shed more details on how you fixed it ? It seems I am having the same issue.

Test the client e.g. by fetching the free asset balance. If you get an error, first make sure the api is reachable and then make a new client instance. This is how I worked around this issue which occurs for me till this day with the latest python-binance version.

Thanks for the details. I did something similar (close the existing connection and open a new one in a loop) that works now for me as well. Cheers.

mndiarra avatar Mar 29 '25 15:03 mndiarra