Skip to content

Commit

Permalink
[TEST] Shut down the event loop with a separate exception
Browse files Browse the repository at this point in the history
  • Loading branch information
puddly committed Jan 3, 2024
1 parent 4e5df94 commit d155888
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion bellows/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import logging
import sys


class EventLoopShuttingDown(RuntimeError):
pass


LOGGER = logging.getLogger(__name__)


Expand Down Expand Up @@ -59,11 +64,16 @@ def force_stop(self):
if self.loop is None:
return

LOGGER.debug("Shutting down thread")

def cancel_tasks_and_stop_loop():
tasks = asyncio.all_tasks(loop=self.loop)

for task in tasks:
self.loop.call_soon_threadsafe(task.cancel)
coro = task.get_coro()

if coro is not None:
self.loop.call_soon_threadsafe(coro.throw, EventLoopShuttingDown())

gather = asyncio.gather(*tasks, return_exceptions=True)
gather.add_done_callback(
Expand Down

0 comments on commit d155888

Please sign in to comment.