python-trio / python-trio/trio

run_fn_as_system_task does not account for nested Trio

Open
#2,191 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
7.3k
Forks
431
Avg merge
2d 17h
Merged PRs (30d)
6

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the deadlock check in trio/_threads.py around line 234 and reproduce the nested-task example from the issue. Trace how run_fn_as_system_task distinguishes the caller from other Trio tasks; done means the portal no longer exits prematurely for the nested case while genuine same-task deadlocks remain prevented.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.