Skip to content

Commit d20305b

Browse files
authored
ASYNC120 docs
1 parent 0688d71 commit d20305b

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

docs/rules.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ _`ASYNC120` : await-in-except
8585
If this checkpoint is cancelled, the current active exception will be replaced by the ``Cancelled`` exception, and cannot be reraised later.
8686
This will not trigger when :ref:`ASYNC102 <ASYNC102>` does, and if you don't care about losing non-cancelled exceptions you could disable this rule.
8787
This is currently not able to detect asyncio shields.
88+
89+
To handle this correctly, use a shielded cancel scope with a timeout::
90+
91+
try:
92+
await process()
93+
except Exception:
94+
with trio.fail_after(seconds, shield=True):
95+
await cleanup()
96+
raise
97+
98+
The shield prevents cancellation from replacing the current exception, while the timeout ensures cleanup can't block indefinitely. Use :func:`trio.move_on_after` if you want to suppress timeout errors rather than raise them.
8899

89100
_`ASYNC121`: control-flow-in-taskgroup
90101
`return`, `continue`, and `break` inside a :ref:`taskgroup_nursery` can lead to counterintuitive behaviour. Refactor the code to instead cancel the :ref:`cancel_scope` inside the TaskGroup/Nursery and place the statement outside of the TaskGroup/Nursery block. In asyncio a user might expect the statement to have an immediate effect, but it will wait for all tasks to finish before having an effect. See `Trio issue #1493 <https://github.com/python-trio/trio/issues/1493>`_ for further issues specific to trio/anyio.

0 commit comments

Comments
 (0)