Skip to content

Commit eb7c901

Browse files
committed
Relax overaggressive deadlock check in from_thread.run(_sync)?
1 parent 0b84a5f commit eb7c901

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

newsfragments/2191.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.

trio/_threads.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,18 @@ def _run_fn_as_system_task(cb, fn, *args, context, trio_token=None):
245245
"this thread wasn't created by Trio, pass kwarg trio_token=..."
246246
)
247247

248-
# Avoid deadlock by making sure we're not called from Trio thread
248+
# Avoid deadlock by making sure we're not called from the same Trio thread we're
249+
# trying to enter
249250
try:
250-
trio.lowlevel.current_task()
251+
current_trio_token = trio.lowlevel.current_trio_token()
251252
except RuntimeError:
252253
pass
253254
else:
254-
raise RuntimeError("this is a blocking function; call it from a thread")
255+
if trio_token == current_trio_token:
256+
raise RuntimeError(
257+
"this is a blocking function; using it to re-enter the current Trio "
258+
"thread would cause a deadlock"
259+
)
255260

256261
q = stdlib_queue.SimpleQueue()
257262
trio_token.run_sync_soon(context.run, cb, q, fn, args)

0 commit comments

Comments
 (0)