From 498cd7e3a2a25913e42700a356a4946e38da26ad Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Thu, 10 Jun 2021 14:02:12 -0400 Subject: [PATCH] Move debugger wait inside OCA nursery --- tractor/_trionics.py | 46 +++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/tractor/_trionics.py b/tractor/_trionics.py index 2e785f0..50cfa30 100644 --- a/tractor/_trionics.py +++ b/tractor/_trionics.py @@ -12,7 +12,7 @@ import trio from async_generator import asynccontextmanager from . import _debug -from ._state import current_actor, is_main_process +from ._state import current_actor, is_main_process, is_root_process from .log import get_logger, get_loglevel from ._actor import Actor from ._portal import Portal @@ -254,6 +254,26 @@ async def _open_and_supervise_one_cancels_all_nursery( "to complete" ) except BaseException as err: + + if is_root_process() and ( + type(err) in { + Exception, trio.MultiError, trio.Cancelled + } + ): + # if we error in the root but the debugger is + # engaged we don't want to prematurely kill (and + # thus clobber access to) the local tty streams. + # instead try to wait for pdb to be released before + # tearing down. + debug_complete = _debug._pdb_complete + if debug_complete and not debug_complete.is_set(): + log.warning( + "Root has errored but pdb is active..waiting " + "on debug lock") + await _debug._pdb_complete.wait() + + # raise + # if the caller's scope errored then we activate our # one-cancels-all supervisor strategy (don't # worry more are coming). @@ -368,25 +388,11 @@ async def open_nursery( async with open_root_actor(**kwargs) as actor: assert actor is current_actor() - try: - async with _open_and_supervise_one_cancels_all_nursery( - actor - ) as anursery: - yield anursery - - except (Exception, trio.MultiError, trio.Cancelled): - # if we error in the root but the debugger is - # engaged we don't want to prematurely kill (and - # thus clobber access to) the local tty streams. - # instead try to wait for pdb to be released before - # tearing down. - if not _debug._pdb_complete.is_set(): - log.warning( - "Root has errored but pdb is active..waiting " - "on debug lock") - await _debug._pdb_complete.wait() - - raise + # try: + async with _open_and_supervise_one_cancels_all_nursery( + actor + ) as anursery: + yield anursery else: # sub-nursery case