Skip to content

Commit 96e45c5

Browse files
Apply suggestions from code review
Documentation clarifications Fix function name typo leading to missed coverage Co-authored-by: Joshua Oreman <[email protected]>
1 parent 2f79f15 commit 96e45c5

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

docs/source/reference-core.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,8 +1826,8 @@ to spawn a child thread, and then use a :ref:`memory channel
18261826
.. note::
18271827

18281828
The ``from_thread.run*`` functions reuse the host task that called
1829-
:func:`trio.to_thread.run_sync` to run your provided function in the typical case,
1830-
namely when ``cancellable=False`` so Trio can be sure that the task will always be
1829+
:func:`trio.to_thread.run_sync` to run your provided function, as long as you're
1830+
using the default ``cancellable=False`` so Trio can be sure that the task will remain
18311831
around to perform the work. If you pass ``cancellable=True`` at the outset, or if
18321832
you provide a :class:`~trio.lowlevel.TrioToken` when calling back in to Trio, your
18331833
functions will be executed in a new system task. Therefore, the

trio/_threads.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def run_in_system_nursery(self, token: TrioToken) -> None:
119119
def in_trio_thread() -> None:
120120
try:
121121
trio.lowlevel.spawn_system_task(
122-
self.run, name=self.afn, context=self.context
122+
self.run_system, name=self.afn, context=self.context
123123
)
124124
except RuntimeError: # system nursery is closed
125125
self.queue.put_nowait(
@@ -394,10 +394,16 @@ def from_thread_check_cancelled() -> None:
394394
395395
.. note::
396396
397-
The check for cancellation attempts of ``cancellable=False`` threads is
398-
interrupted while executing ``from_thread.run*`` functions, which can lead to
399-
edge cases where this function may raise or not depending on the timing of
400-
:class:`~trio.CancelScope` shields being raised or lowered in the Trio threads.
397+
To be precise, :func:`~trio.from_thread.check_cancelled` checks whether the task
398+
running :func:`trio.to_thread.run_sync` has ever been cancelled since the last
399+
time it was running a :func:`trio.from_thread.run` or :func:`trio.from_thread.run_sync`
400+
function. It may raise `trio.Cancelled` even if a cancellation occurred that was
401+
later hidden by a modification to `trio.CancelScope.shield` between the cancelled
402+
`~trio.CancelScope` and :func:`trio.to_thread.run_sync`. This differs from the
403+
behavior of normal Trio checkpoints, which raise `~trio.Cancelled` only if the
404+
cancellation is still active when the checkpoint executes. The distinction here is
405+
*exceedingly* unlikely to be relevant to your application, but we mention it
406+
for completeness.
401407
"""
402408
try:
403409
raise_cancel = PARENT_TASK_DATA.cancel_register[0]
@@ -456,7 +462,10 @@ def from_thread_run(
456462
RunFinishedError: if the corresponding call to :func:`trio.run` has
457463
already completed, or if the run has started its final cleanup phase
458464
and can no longer spawn new system tasks.
459-
Cancelled: if the task enters cancelled status or call to :func:`trio.run`
465+
Cancelled: If the original call to :func:`trio.to_thread.run_sync` is cancelled
466+
(if *trio_token* is None) or the call to :func:`trio.run` completes
467+
(if *trio_token* is not None) while ``afn(*args)`` is running,
468+
then *afn* is likely to raise
460469
completes while ``afn(*args)`` is running, then ``afn`` is likely to raise
461470
:exc:`trio.Cancelled`.
462471
RuntimeError: if you try calling this from inside the Trio thread,

0 commit comments

Comments
 (0)