🐛 Fix syncify
with raise_sync_error=False
on AnyIO 4.x.x, do not start new event loops unnecessarily
#130
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🐛 Fix
syncify
withraise_sync_error=False
on AnyIO 4.x.x, do not start new event loops unnecessarilyCurrently, 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
withcurrent_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).