trio icon indicating copy to clipboard operation
trio copied to clipboard

run_fn_as_system_task does not account for nested Trio

Open mastercoms opened this issue 4 years ago • 0 comments

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.

mastercoms avatar Dec 08 '21 17:12 mastercoms