From d59e9edec8b3cc74fb3fb23545dcd34f52209651 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Mon, 10 May 2021 07:23:39 -0400 Subject: [PATCH] Be more pedantic with error handling --- tractor/_portal.py | 44 +++++++++++++++++++++++++------------------- tractor/_trionics.py | 4 ++-- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/tractor/_portal.py b/tractor/_portal.py index 8944489..0ce1a53 100644 --- a/tractor/_portal.py +++ b/tractor/_portal.py @@ -358,33 +358,39 @@ class Portal: fn_mod_path, fn_name = func_deats(func) - cid, recv_chan, functype, first_msg = await self._submit( - fn_mod_path, fn_name, kwargs) - assert functype == 'context' - - msg = await recv_chan.receive() try: - # the "first" value here is delivered by the callee's - # ``Context.started()`` call. - first = msg['started'] + cid, recv_chan, functype, first_msg = await self._submit( + fn_mod_path, fn_name, kwargs) - except KeyError: - assert msg.get('cid'), ("Received internal error at context?") + assert functype == 'context' + msg = await recv_chan.receive() - if msg.get('error'): - # raise the error message - raise unpack_error(msg, self.channel) - else: - raise - try: + try: + # the "first" value here is delivered by the callee's + # ``Context.started()`` call. + first = msg['started'] + + except KeyError: + assert msg.get('cid'), ("Received internal error at context?") + + if msg.get('error'): + # raise the error message + raise unpack_error(msg, self.channel) + else: + raise + + # deliver context instance and .started() msg value in open + # tuple. ctx = Context(self.channel, cid, _portal=self) - yield ctx, first + try: + yield ctx, first + + finally: + await ctx.cancel() finally: await recv_chan.aclose() - await ctx.cancel() - @dataclass class LocalPortal: diff --git a/tractor/_trionics.py b/tractor/_trionics.py index dcf7aa5..894ab7d 100644 --- a/tractor/_trionics.py +++ b/tractor/_trionics.py @@ -357,7 +357,8 @@ async def open_nursery( try: if actor is None and is_main_process(): - # if we are the parent process start the actor runtime implicitly + # if we are the parent process start the + # actor runtime implicitly log.info("Starting actor runtime!") # mark us for teardown on exit @@ -376,7 +377,6 @@ async def open_nursery( async with _open_and_supervise_one_cancels_all_nursery( actor ) as anursery: - yield anursery finally: