Rapptz / discord.py

An API wrapper for Discord written in Python.
http://discordpy.rtfd.org/en/latest
MIT License
14.82k stars 3.75k forks source link

RuntimeError: There is no current event loop in thread #5887

Closed Funeoz closed 4 years ago

Funeoz commented 4 years ago

Summary

I had a working bot with threads that send an embed when they receive data from a socket connection. So when it receives data, a background task is created that at the end will send the embed. But it somehow stopped working. The only change to my bot was updating discord.py to 1.5.0.

Reproduction Steps

No real reproduction steps. It happened I think just after updating discord.py. I tried downgrading to 1.4.2 but I get the same RuntimeError.

Expected Results

To work as normal.

Actual Results

Exception in thread Thread-523:
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/home/theo/Esport-notifier/env/lib/python3.8/site-packages/socketio/client.py", line 616, in _handle_eio_message
    self._handle_event(pkt.namespace, pkt.id, pkt.data)
  File "/home/theo/Esport-notifier/env/lib/python3.8/site-packages/socketio/client.py", line 500, in _handle_event
    r = self._trigger_event(data[0], namespace, *data[1:])
  File "/home/theo/Esport-notifier/env/lib/python3.8/site-packages/socketio/client.py", line 550, in _trigger_event
    return self.handlers[namespace][event](*args)
  File "/home/theo/Esport-notifier/py/csgo/scorebot/scorebot.py", line 219, in log
    self.on_event[event](event_data)
  File "/home/theo/Esport-notifier/py/cogs/live_score.py", line 79, in on_round_end
    self.send_embed.start()
  File "/home/theo/Esport-notifier/env/lib/python3.8/site-packages/discord/ext/tasks/__init__.py", line 191, in start
    self.loop = asyncio.get_event_loop()
  File "/usr/lib/python3.8/asyncio/events.py", line 639, in get_event_loop
    raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-523'.

Checklist

System Information

This is the system info from my PC but it currently runs on heroku and I get the same error.

Rapptz commented 4 years ago

asyncio has never been threadsafe. If you're on another thread without a running event loop you have to create a new one and then set it as the event loop policy.