Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError: Event loop is closed #6399

Closed
thetradinggoose opened this issue Feb 13, 2022 · 13 comments
Closed

RuntimeError: Event loop is closed #6399

thetradinggoose opened this issue Feb 13, 2022 · 13 comments
Labels
Triage Needed Issues yet to verify

Comments

@thetradinggoose
Copy link

thetradinggoose commented Feb 13, 2022

Describe your environment

  • Operating system: Windows 10 Home
  • Python Version: Python 3.10.2
  • CCXT version: 1.72.36
  • Freqtrade Version: 2022.1

Describe the problem:

The bot is running fine, but occasionally and specially when reloading the config , it logs
...\Python310\lib\asyncio\base_events.py", line 510, in _check_closed raise RuntimeError('Event loop is closed') RuntimeError: Event loop is closed

Its not just the base_events.py but all files in the asyncio folder is getting this RuntimeError: Event loop is closed

Observed Results:

  • What happened?

    • It happens from time to time specially when reloading the config via telegram or restarting the bot via the command line. Im using Powershell btw since I'm on a Windows machine.
  • What did you expect to happen?

    • I just let it run and after awhile it goes back to running state again.

Relevant code exceptions or logs

2022-02-13 15:09:07,939 - freqtrade.rpc.rpc_manager - INFO - Sending rpc message: {'type': status, 'status': 'config reloaded'}
2022-02-13 15:09:08,968 - freqtrade.worker - INFO - Changing state from RELOAD_CONFIG to: RUNNING
2022-02-13 15:09:08,968 - freqtrade.rpc.rpc_manager - INFO - Sending rpc message: {'type': warning, 'status': 'Dry run is enabled. All trades are simulated.'}
...
Exception ignored in: <function _ProactorBasePipeTransport.del at 0x0000018F554FE3B0>
Traceback (most recent call last):
  File "C:\Users\thetradinggoose\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 116, in del
    self.close()
  File "C:\Users\thetradinggoose\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\thetradinggoose\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 745, in call_soon
    self._check_closed()
  File "C:\Users\thetradinggoose\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Exception ignored in: <function _ProactorBasePipeTransport.del at 0x0000018F554FE3B0>
Traceback (most recent call last):
  File "C:\Users\thetradinggoose\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 116, in del
    self.close()
  File "C:\Users\thetradinggoose\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\thetradinggoose\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 745, in call_soon
    self._check_closed()
  File "C:\Users\thetradinggoose\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Exception ignored in: <function _ProactorBasePipeTransport.del at 0x0000018F554FE3B0>
Traceback (most recent call last):
  File "C:\Users\thetradinggoose\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 116, in del
    self.close()
  File "C:\Users\thetradinggoose\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\thetradinggoose\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 745, in call_soon
    self._check_closed()
  File "C:\Users\thetradinggoose\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Exception ignored in: <function _ProactorBasePipeTransport.del at 0x0000018F554FE3B0>
Traceback (most recent call last):
  File "C:\Users\thetradinggoose\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 116, in del
    self.close()
  File "C:\Users\thetradinggoose\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\thetradinggoose\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 745, in call_soon
    self._check_closed()
  File "C:\Users\thetradinggoose\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Exception ignored in: <function _ProactorBasePipeTransport.del at 0x0000018F554FE3B0>
Traceback (most recent call last):
  File "C:\Users\thetradinggoose\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 116, in del
    self.close()
  File "C:\Users\thetradinggoose\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
File "C:\Users\thetradinggoose\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 745, in call_soon
    self._check_closed()
  File "C:\Users\thetradinggoose\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
@thetradinggoose thetradinggoose added the Triage Needed Issues yet to verify label Feb 13, 2022
@xmatthias
Copy link
Member

Please provide a full stacktrace, not just one line of logs.

Based on the provided information, there is nothing we can do as it's unclear where the problem originates.

@thetradinggoose
Copy link
Author

@xmatthias I have updated my issue and include the full logs. thanks

@xmatthias
Copy link
Member

xmatthias commented Feb 13, 2022

I notice you're running 3.10 ... I'd advise to not use it for the moment.
while we do in theory support it - it did show a memory leak (for no good reason) when using it - downgrading to 3.9 (without ANY further action) did fix that memory leak.

I'd be curious if it happens on 3.9 as well - as i've not encountered this yet (might be another 3.10 oddity) - or even a windows oddity ... (i've never used windows natively (without WSL) to run freqtrade - and asyncio handling on windows IS slightly crippled by how windows works)

@thetradinggoose
Copy link
Author

Ok, I will try to reproduce the issue using 3.9 and will give you feedback asap. thanks again for your time.

@xmatthias
Copy link
Member

@thetradinggoose Did this work better on 3.9? or did you encounter the same issue again?

@xmatthias
Copy link
Member

I'll assume this is fixed now by using 3.9.

Let me know if that's not the case

@lapitskiy
Copy link

RuntimeError: Event loop is closed

i have this problem with python 3.9

(fabletrade) PS C:\python\freqtrade> freqtrade download-data --config config.json --days 21 -t 5m
2023-04-02 05:34:14,036 - freqtrade - INFO - freqtrade 2023.3
2023-04-02 05:34:14,036 - freqtrade.configuration.load_config - INFO - Using config: config.json ...
2023-04-02 05:34:14,037 - freqtrade.loggers - INFO - Verbosity set to 0
2023-04-02 05:34:14,038 - freqtrade.configuration.configuration - INFO - Using user-data directory: C:\python\freqtrade\user_data ...
2023-04-02 05:34:14,038 - freqtrade.configuration.configuration - INFO - Using data directory: C:\python\freqtrade\user_data\data\binance ...
2023-04-02 05:34:14,038 - freqtrade.configuration.configuration - INFO - timeframes --timeframes: ['5m']
2023-04-02 05:34:14,038 - freqtrade.configuration.configuration - INFO - Detected --days: 21
2023-04-02 05:34:14,040 - freqtrade.exchange.check_exchange - INFO - Checking exchange...
2023-04-02 05:34:14,052 - freqtrade.exchange.check_exchange - INFO - Exchange "binance" is officially supported by the Freqtrade development team.
2023-04-02 05:34:14,052 - freqtrade.configuration.configuration - INFO - Using pairlist from configuration.
2023-04-02 05:34:14,052 - freqtrade.configuration.config_validation - INFO - Validating configuration ...
2023-04-02 05:34:14,059 - freqtrade.exchange.exchange - INFO - Instance is running with dry_run enabled
2023-04-02 05:34:14,060 - freqtrade.exchange.exchange - INFO - Using CCXT 3.0.37
2023-04-02 05:34:14,083 - freqtrade.exchange.exchange - INFO - Using Exchange "Binance"
2023-04-02 05:34:14,083 - freqtrade.resolvers.exchange_resolver - INFO - Using resolved exchange 'Binance'...
2023-04-02 05:34:14,083 - freqtrade.exchange.exchange - INFO - Markets were not loaded. Loading them now..
2023-04-02 05:34:18,244 - freqtrade.commands.data_commands - INFO - About to download pairs: ['BTC/USDT'], intervals: ['5m'] to C:\python\freqtrade\user_data\data\binance
2023-04-02 05:34:18,247 - freqtrade.data.history.history_utils - INFO - Downloading pair BTC/USDT, interval 5m.
2023-04-02 05:34:18,272 - freqtrade.data.history.history_utils - INFO - (1/1) - Download history data for "BTC/USDT", 5m, spot and store in C:\python\freqtrade\user_data\data\binance. From 2023-04-02T05:10:00 to now
2023-04-02 05:34:18,580 - freqtrade.exchange.exchange - INFO - Downloaded data for BTC/USDT with length 5.
Exception ignored in: <function _ProactorBasePipeTransport.del at 0x000001BB00655EE0>
Traceback (most recent call last):
File "C:\Users\Egor\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 116, in del
self.close()
File "C:\Users\Egor\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 108, in close
self._loop.call_soon(self._call_connection_lost, None)
File "C:\Users\Egor\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 746, in call_soon
self._check_closed()
File "C:\Users\Egor\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 510, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Exception ignored in: <function _ProactorBasePipeTransport.del at 0x000001BB00655EE0>
Traceback (most recent call last):
File "C:\Users\Egor\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 116, in del
self.close()
File "C:\Users\Egor\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 108, in close
self._loop.call_soon(self._call_connection_lost, None)
File "C:\Users\Egor\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 746, in call_soon
self._check_closed()
File "C:\Users\Egor\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 510, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Exception ignored in: <function _ProactorBasePipeTransport.del at 0x000001BB00655EE0>
Traceback (most recent call last):
File "C:\Users\Egor\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 116, in del
self.close()
File "C:\Users\Egor\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 108, in close
self._loop.call_soon(self._call_connection_lost, None)
File "C:\Users\Egor\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 746, in call_soon
self._check_closed()
File "C:\Users\Egor\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 510, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

@xmatthias
Copy link
Member

Make sure to use the latest available python version (not 3.9.2, but 3.9.xx ...).

if you still have this problem, please open a NEW issue - filling out the issue template>

@theluxaz
Copy link
Contributor

I was still having this error on Python 3.9.13 when processing long time ranges.
Adding "time.sleep(0.1)" in the finally statement in main() function fixed this.

Got the solution from here:
encode/httpx#914

Using Windows 10 and newest dev release of Freqtrade.

@xmatthias
Copy link
Member

@theluxaz the linked issue ALSO mentions that this is supposed to be solved in the most recent python version (3.9.16 atm) ... so i suspect that a simple python upgrade would/will suffice.

@theluxaz
Copy link
Contributor

Did some more testing and found out that neither time.sleep() or upgrading to 3.9.16 fixes the problem. Please ignore my previous post.
In my case the error happens only AFTER the first running of the main() function (second and so on). The first time it never errors. This may be due to me commenting out
sys.exit(return_code)
for easier backtesting. Not sure if this is the case for other people.

Thankfully the program still works fine, it's only visual from what I can tell.

Exception ignored in: <function _ProactorBasePipeTransport.del at 0x000001E91294AC10>
Traceback (most recent call last):
File "C:\Program Files\Python39\lib\asyncio\proactor_events.py", line 116, in del
self.close()
File "C:\Program Files\Python39\lib\asyncio\proactor_events.py", line 108, in close
self._loop.call_soon(self._call_connection_lost, None)
File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 751, in call_soon
self._check_closed()
File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 515, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Exception ignored in: <function _ProactorBasePipeTransport.del at 0x000001E91294AC10>
Traceback (most recent call last):
File "C:\Program Files\Python39\lib\asyncio\proactor_events.py", line 116, in del
self.close()
File "C:\Program Files\Python39\lib\asyncio\proactor_events.py", line 108, in close
self._loop.call_soon(self._call_connection_lost, None)
File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 751, in call_soon
self._check_closed()
File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 515, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

@xmatthias
Copy link
Member

Quite honestly, the best solution for this is to use WSL2 (since you're on windows).

@Skinok
Copy link

Skinok commented Jun 5, 2023

You can put

if os.name == 'nt':
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

at the beginning of the exchange/common.py file.
It fixes this issue when download data (for me).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Triage Needed Issues yet to verify
Projects
None yet
Development

No branches or pull requests

5 participants