python-trio / python-trio/trio

`from_thread.run(_sync)?` fails in any thread running Trio

Open
#2,534 8 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

hi! from_thread.run(_sync)? uses a heuristic to prevent the user from accidentally deadlocking their event loop by writing code like https://github.com/python-trio/trio/pull/1574/files#r435039316.

the heuristic is currently much more aggressive than is required to prevent this deadlock, however. it has the side-effect[^1] of preventing one from calling from_thread.run(_sync)? in any thread running trio, even though such calls are almost always safe and deadlock-free.

[^1]: citing the RuntimeErrors reported in from_thread.run(_sync)?'s docstrings, this would appear to be an unintended side-effect of the current heuristic.

it would be useful to be able to call from_thread.run(_sync)? from trio threads[^2], but the current heuristic blindly prevents this. i'd like to propose relaxing from_thread.run(_sync)? to allow calling from_thread.run(_sync)? from trio threads. (one use-case of this is https://github.com/agronholm/anyio/issues/525.)

[^2]: for example in order to call Nursery.start_soon in a thread-safe manner.

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 with the from_thread.run(_sync)? implementation and its docstrings, then review the heuristic that raises RuntimeError when called from Trio threads. Check how the proposed relaxation affects calls such as Nursery.start_soon and verify that the documented deadlock prevention still holds. Done means safe calls from Trio threads are allowed without weakening the intended protection.

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.