Skip to content

run_fn_as_system_task does not account for nested Trio #2191

Open
@mastercoms

Description

@mastercoms

I'm new to Trio, so forgive me if I get the terminology wrong. This check for deadlock only checks if there is a Trio task at all, not that the task is the same as the caller. So, if we have something like this:

from concurrent.futures import as_completed
from anyio.from_thread import start_blocking_portal

async def endpoint():
  with start_blocking_portal() as portal:
    futures = [portal.start_task_soon(long_running_task, i) for i in range(1, 5)]
    for future in as_completed(futures):
        print(future.result())

It will fail and exit the portal prematurely because endpoint is running in Trio, but the portal is a different Trio task. The check sees if there's a task at all, rather than checking if the call would actually deadlock the task in the case of nesting.

If I remove the current task check, the portal runs successfully with no deadlock.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions