Check if main event loop is running before scheduling#528
Conversation
Fixes django#525 After some debugging, I found that `main_event_loop` is sometimes closed. If it's closed, we should not `call_soon_threadsafe`
|
If the user called |
|
It's already "failing to stop" now due to a potential deadlock, do you have another solution? |
|
Is there a reason the answer is any more complicated than “don’t call |
|
In my case, [TEST1] main event loop <_UnixSelectorEventLoop running=True closed=False debug=False>
in main event loop, running? True
exc_info (None, None, None) awaitable <coroutine object convert_exception_to_response.<locals>.inner at 0x123d415d0>
result <HttpResponse status_code=401, "application/json">
running_in_main_event_loop True
PASSED
[TEST2] main event loop <_UnixSelectorEventLoop running=True closed=False debug=False>
in main event loop, running? True
exc_info (None, None, None) awaitable <coroutine object convert_exception_to_response.<locals>.inner at 0x122c91f30>
result <HttpResponse status_code=200, "application/json; charset=utf-8">
running_in_main_event_loop True
main event loop <_UnixSelectorEventLoop running=False closed=False debug=False>
in main event loop, running? False
[...stuck...] |
|
Event loops don’t arbitrarily stop themselves. You can look at the code here: https://github.com/python/cpython/blob/v3.13.7/Lib/asyncio/base_events.py The only way for Without a reproducible test case, there’s not really anything more I can say. |
|
I misinterpreted. |
|
I see, if loop is not running, |
Fixes #525
After some debugging, I found that
main_event_loopis sometimes closed. If it's closed, we should notcall_soon_threadsafe