diff --git a/tractor/_portal.py b/tractor/_portal.py index 8148a5d9..928a8b9c 100644 --- a/tractor/_portal.py +++ b/tractor/_portal.py @@ -42,7 +42,7 @@ from async_generator import asynccontextmanager from .trionics import maybe_open_nursery from .devx import ( # acquire_debug_lock, - # pause, + pause, maybe_wait_for_debugger, ) from ._state import ( @@ -556,7 +556,7 @@ class Portal: # placeholder for any exception raised in the runtime # or by user tasks which cause this context's closure. scope_err: BaseException|None = None - ctxc_from_callee: ContextCancelled|None = None + ctxc_from_callee: ContextCancelled|bool = False try: async with trio.open_nursery() as nurse: @@ -673,8 +673,6 @@ class Portal: # `Nursery.cancel_scope.cancel()`) except ContextCancelled as ctxc: scope_err = ctxc - ctxc_from_callee = ctxc - # XXX TODO XXX: FIX THIS debug_mode BUGGGG!!! # using this code and then resuming the REPL will # cause a SIGINT-ignoring HANG! @@ -686,6 +684,10 @@ class Portal: # # await pause() + # ctxc_from_callee: bool = ( + # ctxc.src_actor_uid == self.chan.uid + # ) + # CASE 2: context was cancelled by local task calling # `.cancel()`, we don't raise and the exit block should # exit silently. @@ -696,6 +698,7 @@ class Portal: and ctxc.canceller == self.actor.uid ): + ctxc_from_callee = True log.cancel( f'Context (cid=[{ctx.cid[-6:]}..] cancelled gracefully with:\n' f'{ctxc}' @@ -755,6 +758,7 @@ class Portal: f'{caller_err}\n' ) + # TODO: does this need to be shielded? if debug_mode(): # async with acquire_debug_lock(self.actor.uid): # pass @@ -771,12 +775,14 @@ class Portal: 'Calling `ctx.cancel()`!\n' ) - # we don't need to cancel the callee if it already # told us it's cancelled ;p - if ctxc_from_callee is None: + if not ctxc_from_callee: + log.critical('SENDING CANCEL') try: - await ctx.cancel() + # await pause(shield=True) + with trio.CancelScope(shield=True): + await ctx.cancel() except ( trio.BrokenResourceError, trio.ClosedResourceError,