Check if main event loop is running before scheduling#528
Conversation
|
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, |
|
The problem is still there and can be fixed via this one-line change |
|
@kigawas Can you add a minimal test case that fails without this change to demonstrate the issue? (It's not clear why you're seeing what you are.) |
|
@carltongibson Please see this repo |
|
Test added |
|
Your code mutates the private variable Is there some misbehavior you can demonstrate using only the public interface? |
|
I encountered this problem when running pytest-asyncio in a Django project which has some sync_to_async and async_to_sync mixture, not tinkering its private API. |
|
Okay, then, can you show a reproduction recipe using the public API? |
This comment has been minimized.
This comment has been minimized.
|
@kigawas your last comment goes beyond the code of conduct. Please be civil. Or you will be bared from the organisation. This is your final warning. |
|
@andersk just to underline, your contributions here are always of high quality and are greatly appreciated. Sorry you have to take negative comments 🎁 |
This comment was marked as low quality.
This comment was marked as low quality.
The revamped drop-in replacement for asgiref: https://github.com/kigawas/asgire
Fixes #525
After some debugging, I found that
main_event_loopis sometimes closed. If it's closed, we should notcall_soon_threadsafe