From 853fe8a1a1c5427f9f2524155894e793858981a6 Mon Sep 17 00:00:00 2001 From: Weiliang Li Date: Wed, 10 Sep 2025 04:26:18 +0900 Subject: [PATCH] Check if main event loop is running before scheduling Fixes #525 After some debugging, I found that `main_event_loop` is sometimes closed. If it's closed, we should not `call_soon_threadsafe` --- asgiref/sync.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asgiref/sync.py b/asgiref/sync.py index 54496efe..ffe6047f 100644 --- a/asgiref/sync.py +++ b/asgiref/sync.py @@ -231,7 +231,7 @@ async def new_loop_wrap() -> None: finally: del self.loop_thread_executors[loop] - if self.main_event_loop is not None: + if self.main_event_loop is not None and self.main_event_loop.is_running(): try: self.main_event_loop.call_soon_threadsafe( self.main_event_loop.create_task, awaitable