Skip to content

cover anyio’s NoEventLoopError without altering existing behavior#145

Open
gotsysdba wants to merge 1 commit intooracle:mainfrom
gotsysdba:114_anyio
Open

cover anyio’s NoEventLoopError without altering existing behavior#145
gotsysdba wants to merge 1 commit intooracle:mainfrom
gotsysdba:114_anyio

Conversation

@gotsysdba
Copy link
Member

Fixes #144

Note once the minimal version of anyio ≥ 4.11, the backward compatibility code can be removed:


# anyio.NoEventLoopError was added in anyio 4.11. anyio 4.10 is still supported
# (via the langgraph/evaluation extras), where the attribute does not exist.
# Once the minimum anyio version is ≥ 4.11, replace this with a plain
# tuple and catch anyio.NoEventLoopError directly (commented out below)
_NO_LOOP_ERRORS: tuple[type[BaseException], ...] = (AsyncLibraryNotFoundError,)
if hasattr(anyio, "NoEventLoopError"):
    _NO_LOOP_ERRORS = (*_NO_LOOP_ERRORS, anyio.NoEventLoopError)


def get_execution_context() -> AsyncContext:
    """
    Return one of:
    - 'sync'         → plain synchronous context (no loop, no worker thread)
    - 'sync_worker'  → synchronous worker thread (spawned by to_thread.run_sync)
    - 'async'        → running inside the event loop
    """
    try:
        anyio.get_current_task()
        return AsyncContext.ASYNC
    # except (AsyncLibraryNotFoundError, anyio.NoEventLoopError): # anyio>4.10
    except _NO_LOOP_ERRORS:

Replace with:

def get_execution_context() -> AsyncContext:
    """
    Return one of:
    - 'sync'         → plain synchronous context (no loop, no worker thread)
    - 'sync_worker'  → synchronous worker thread (spawned by to_thread.run_sync)
    - 'async'        → running inside the event loop
    """
    try:
        anyio.get_current_task()
        return AsyncContext.ASYNC
    except (AsyncLibraryNotFoundError, anyio.NoEventLoopError):

@gotsysdba gotsysdba requested a review from a team March 21, 2026 06:54
@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Mar 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OCA Verified All contributors have signed the Oracle Contributor Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: get_execution_context() crashes with anyio >= 4.12 due to uncaught NoEventLoopError

1 participant