-
-
Notifications
You must be signed in to change notification settings - Fork 182
Description
Things to check first
-
I have searched the existing issues and didn't find my bug already reported there
-
I have checked that my bug is still present in the latest release
AnyIO version
4.12.0
Python version
3.10.17
What happened?
Hello,
Before version 4.12.0, AnyIO used sniffio to detect available async libraries in the Python environment. Sniffio raised an AsyncLibraryNotFoundError when no async library was available, which was very convenient in some cases (for example, when running asynchronously code in production but synchronously in tests, with asynchronous libs in the classpath).
Starting with version 4.12, sniffio was removed and async library detection is now handled by custom code (PR #1021). However, this introduces two issues:
- The new
NoCurrentAsyncBackendexception, which is raised when no event loop is present, lives in the protected _core module and therefore should not be accessed from outside the library. - The newly added
get_available_backendsfunction only checks which backends are available usingimport; it does not verify whether an event loop is actually running.
I would recommend either exposing this exception so it is accessible to end users, or updating the backend availability check to reflect real availability (i.e., whether it is being called from within an active event loop).
How can we reproduce the bug?
anyio==4.12.0
asyncio==4.0.0
import anyio
if __name__ == '__main__':
print(anyio.get_available_backends()) # ('asyncio',)
anyio.to_thread.current_default_thread_limiter() # Trigger anyio._core._eventloop.NoCurrentAsyncBackend (protected member)