Repository: freqtrade/freqtrade

Issue:
Bot crashing randomly running after few days on docker image on ubuntu
<!-- 
Have you searched for similar issues before posting it?

If you have discovered a bug in the bot, please [search our issue tracker](https://github.com/freqtrade/freqtrade/issues?q=is%3Aissue). 
If it hasn't been reported, please create a new issue.

Please do not use bug reports to request new features.
-->

## Describe your environment

  * Operating system: ____
  * Python Version: _____ (`python -V`)
  * CCXT version: _____ (`pip freeze | grep ccxt`)
  * Freqtrade Version: ____ (`freqtrade -V` or `docker-compose run --rm freqtrade -V` for Freqtrade running in docker) : freqtrade 2020.10
  
Note: All issues other than enhancement requests will be closed without further comment if the above template is deleted or not filled out.

## Describe the problem:

*Explain the problem you have encountered*

### Steps to reproduce:

  1. Run freqtrade bot and keep running 3 to 4 days 

  
### Observed Results:

  *   Randomly its crashing
  *  I am seeing notification on Telegram as process died though my machine is running

### Relevant code exceptions or logs

Note: Please copy/paste text of the messages, no screenshots of logs please.

  ```
  // paste your log here
  File "/freqtrade/freqtrade/commands/trade_commands.py", line 19, in start_trading
    worker.run()
  File "/freqtrade/freqtrade/worker.py", line 74, in run
    state = self._worker(old_state=state)
  File "/freqtrade/freqtrade/worker.py", line 111, in _worker
    self._throttle(func=self._process_running, throttle_secs=self._throttle_secs)
  File "/freqtrade/freqtrade/worker.py", line 132, in _throttle
    result = func(*args, **kwargs)
  File "/freqtrade/freqtrade/worker.py", line 145, in _process_running
    self.freqtrade.process()
  File "/freqtrade/freqtrade/freqtradebot.py", line 152, in process
    self.exchange.reload_markets()
  File "/freqtrade/freqtrade/exchange/exchange.py", line 309, in reload_markets
    self._load_async_markets(reload=True)
  File "/freqtrade/freqtrade/exchange/exchange.py", line 282, in _load_async_markets
    asyncio.get_event_loop().run_until_complete(
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/usr/local/lib/python3.8/site-packages/ccxt/async_support/base/exchange.py", line 179, in load_markets
    raise e
  File "/usr/local/lib/python3.8/site-packages/ccxt/async_support/base/exchange.py", line 175, in load_markets
    result = await self.markets_loading
  File "/usr/local/lib/python3.8/site-packages/ccxt/async_support/base/exchange.py", line 165, in load_markets_helper
    markets = await self.fetch_markets(params)
  File "/usr/local/lib/python3.8/site-packages/ccxt/async_support/binance.py", line 567, in fetch_markets
    response = await getattr(self, method)(query)
  File "/usr/local/lib/python3.8/site-packages/ccxt/async_support/binance.py", line 2403, in request
    response = await self.fetch2(path, api, method, params, headers, body)
  File "/usr/local/lib/python3.8/site-packages/ccxt/async_support/base/exchange.py", line 95, in fetch2
    return await self.fetch(request['url'], request['method'], request['headers'], request['body'])
  File "/usr/local/lib/python3.8/site-packages/ccxt/async_support/base/exchange.py", line 116, in fetch
    async with session_method(yarl.URL(url, encoded=True),
  File "/usr/local/lib/python3.8/site-packages/aiohttp/client.py", line 1124, in __aenter__
    self._resp = await self._coro
  File "/usr/local/lib/python3.8/site-packages/aiohttp/client.py", line 626, in _request
    break
  File "/usr/local/lib/python3.8/site-packages/aiohttp/helpers.py", line 656, in __exit__
    raise asyncio.TimeoutError from None
asyncio.exceptions.TimeoutError
2020-11-08 10:52:19,839 - freqtrade.commands.trade_commands - INFO - worker found ... calling exit
2020-11-08 10:52:20,615 - freqtrade.rpc.rpc_manager - INFO - Sending rpc message: {'type': status, 'status': 'process died'}
2020-11-08 10:52:23,239 - freqtrade.freqtradebot - INFO - Cleaning up modules ...
2020-11-08 10:52:27,169 - freqtrade.rpc.rpc_manager - INFO - Sending rpc message: {'type': warning, 'status': "4 open trades active.\n\nHandle these trades manually on Binance, or '/start' the bot again and use '/stopbuy' to handle open trades gracefully. \nTrades are simulated."}
2020-11-08 10:52:31,178 - freqtrade.rpc.rpc_manager - INFO - Cleaning up rpc modules ...
2020-11-08 10:52:31,386 - apscheduler.scheduler - INFO - Scheduler has been shut down
  ```



Discussion:
---
version: '3'
services:
  freqtrade:
    image: freqtradeorg/freqtrade:stable
    # image: freqtradeorg/freqtrade:develop
    # Use plotting image
    # image: freqtradeorg/freqtrade:develop_plot
    # Build step - only needed when additional dependencies are needed
    # build:
    #   context: .
    #   dockerfile: "./Dockerfile.technical"
    restart: unless-stopped
    container_name: freqtrade_1
    volumes:
      - "./user_data:/freqtrade/user_data"
    # Default command used when running `docker compose up`
    command: >
      trade
      --logfile /freqtrade/user_data/logs/freqtrade.log
      --db-url sqlite:////freqtrade/user_data/tradesv3.sqlite
      --config /freqtrade/user_data/config.json
      --strategy SampleStrategy
While running i execute the following command:
sudo docker-compose run --rm freqtrade trade --config user_data/my_trade_config.json --strategy MyTradeStrategy
based on the exception, it's a timeout error when doing a network call. 

Best check your network connections - maybe it's down / dropping at this point in time, or is slow for some other reasons (or slow in general, and "extra slow" when it's crashing.

Now the bot should not crash for sure - but the root of the problem is to do with your network, not with the bot itself.
Actually i am running this on my cloud machine so ideally we suppose to have network all the time and moreover i able to connect machine from my local terminal too. Ideally we should not throw exception and come out immediately in case of one time failed. Potentially we can retry next till some max number of time to avoid such kind of issue. if in case network comes up we should be fine. Just a thought. thanks
don't think i don't know that it shouldn't crash :laughing: 

still, a timeout from a VPS should be considered bad - maybe it's overloaded? As i've never had a timeout out of this method once since i run bots (and that's for years now ...) 
are you able to check if your server was high in cpu (or memory) at the time of the crash?

Resolving patch:
diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py
--- a/freqtrade/exchange/exchange.py
+++ b/freqtrade/exchange/exchange.py
@@ -282,7 +282,7 @@ def _load_async_markets(self, reload: bool = False) -> None:
                 asyncio.get_event_loop().run_until_complete(
                     self._api_async.load_markets(reload=reload))
 
-        except ccxt.BaseError as e:
+        except (asyncio.TimeoutError, ccxt.BaseError) as e:
             logger.warning('Could not load async markets. Reason: %s', e)
             return
 


Test patch: