From 6476df2c69de5b5be7ce93e7e15d2340a3e99a75 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Thu, 10 Dec 2020 13:48:40 -0500 Subject: [PATCH] Raise any asyncio errors if in trio task on cancel --- tractor/to_asyncio.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/tractor/to_asyncio.py b/tractor/to_asyncio.py index 85600d7..1fa923d 100644 --- a/tractor/to_asyncio.py +++ b/tractor/to_asyncio.py @@ -8,7 +8,6 @@ from typing import ( Callable, AsyncIterator, Awaitable, - Union, ) import trio @@ -91,10 +90,12 @@ async def run_task( def cancel_trio(task): """Cancel the calling ``trio`` task on error. """ - nonlocal err + nonlocal aio_err aio_err = task.exception() + if aio_err: log.exception(f"asyncio task errorred:\n{aio_err}") + cancel_scope.cancel() task.add_done_callback(cancel_trio) @@ -109,6 +110,12 @@ async def run_task( async with from_aio: async for item in from_aio: yield item + + if cancel_scope.cancelled_caught: + # always raise from any captured asyncio error + if aio_err: + raise aio_err + except BaseException as err: if aio_err is not None: # always raise from any captured asyncio error @@ -124,6 +131,11 @@ async def run_task( # return single value return await from_aio.receive() + if cancel_scope.cancelled_caught: + # always raise from any captured asyncio error + if aio_err: + raise aio_err + # Do we need this? except BaseException as err: if aio_err is not None: