await
in a finally
block + cancellation of task group.
#367
-
When scheduling a coroutine in a task group, and cancelling this task, the I'm not entirely sure whether the error is on my end or not. In the code below, the "after finally" print never happens. import asyncio
from anyio import create_task_group
async def func():
try:
await asyncio.sleep(5)
finally:
print("Before finally")
await asyncio.sleep(1)
print("After finally")
async def main():
async with create_task_group() as tg:
tg.start_soon(func)
await asyncio.sleep(1)
tg.cancel_scope.cancel()
asyncio.run(main()) The work-around for me would be to start a normal asyncio task for the cleanup without waiting for it, or shielding it from cancellation, but that doesn't sound like structured concurrency. There are a couple of similar issues, but mostly related to nested task groups. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
This is part of the trio level cancellation semantics eg: https://trio.readthedocs.io/en/stable/reference-core.html#cancellation-semantics
with anyio.move_on_after(TIMEOUT):
conn = make_connection()
try:
await conn.send_hello_msg()
finally:
with anyio.move_on_after(CLEANUP_TIMEOUT) as cleanup_scope:
cleanup_scope.shield = True
await conn.send_goodbye_msg() |
Beta Was this translation helpful? Give feedback.
This is part of the trio level cancellation semantics eg: https://trio.readthedocs.io/en/stable/reference-core.html#cancellation-semantics