Skip to content

Commit

Permalink
Relax overaggressive deadlock check in from_thread.run(_sync)?
Browse files Browse the repository at this point in the history
  • Loading branch information
gschaffner committed Jan 13, 2023
1 parent 0b84a5f commit 260c616
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions newsfragments/2534.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`trio.from_thread.run` and `trio.from_thread.run_sync` no longer raise `RuntimeError` when used to enter a thread running Trio from a different thread running Trio.
11 changes: 8 additions & 3 deletions trio/_threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,18 @@ def _run_fn_as_system_task(cb, fn, *args, context, trio_token=None):
"this thread wasn't created by Trio, pass kwarg trio_token=..."
)

# Avoid deadlock by making sure we're not called from Trio thread
# Avoid deadlock by making sure we're not called from the same Trio thread we're
# trying to enter
try:
trio.lowlevel.current_task()
current_trio_token = trio.lowlevel.current_trio_token()
except RuntimeError:
pass
else:
raise RuntimeError("this is a blocking function; call it from a thread")
if trio_token == current_trio_token:
raise RuntimeError(
"this is a blocking function; using it to re-enter the current Trio "
"thread would cause a deadlock"
)

q = stdlib_queue.SimpleQueue()
trio_token.run_sync_soon(context.run, cb, q, fn, args)
Expand Down

0 comments on commit 260c616

Please sign in to comment.