Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix syncify with raise_sync_error=False on AnyIO 4.x.x, do not start new event loops unnecessarily #130

Merged
merged 7 commits into from Feb 22, 2024

Conversation

tiangolo
Copy link
Owner

🐛 Fix syncify with raise_sync_error=False on AnyIO 4.x.x, do not start new event loops unnecessarily

Currently, when using syncify(do_stuff, raise_sync_error=False)() and AnyIO 4.x.x, when running on an async context (there is a parent main async function, e.g. with FastAPI), it will instead of sending the async task to the async event loop in the main thread as it should, it will start a new event loop for this async task, which is incorrect behavior when there's a parent async function, it's the correct behavior only when there's no async parent function.

This is because AnyIO 4.x.x changed the internal name current_async_module with current_async_backend and Asyncer was only checking for that.

This includes the fix and tests that verify that jumping from async to sync to async and all those possible async sandwiches behave correctly, running things in the correct thread (the main thread with the event loop or worker threads for sync functions under async parents).

…havior, not starting event loops unnecessarily and moving across threads (main to worker) correctly
… fix memory and resource usage by not starting new event loops unecessarily
@tiangolo tiangolo added the bug Something isn't working label Feb 22, 2024
@tiangolo tiangolo merged commit 493d41b into main Feb 22, 2024
17 checks passed
@tiangolo tiangolo deleted the fix-raise_sync_error branch February 22, 2024 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant