From 3fa36f64acc952f653cbff6669a248a2628d4422 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Sun, 13 Jun 2021 20:21:18 -0400 Subject: [PATCH] Wait for debugger lock task context termination --- tractor/_debug.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/tractor/_debug.py b/tractor/_debug.py index 1c0bc8c..82612a8 100644 --- a/tractor/_debug.py +++ b/tractor/_debug.py @@ -161,7 +161,7 @@ async def _hijack_stdin_relay_to_child( ctx: tractor.Context, subactor_uid: Tuple[str, str] -) -> None: +) -> str: global _pdb_complete @@ -179,6 +179,7 @@ async def _hijack_stdin_relay_to_child( async with _acquire_debug_lock(subactor_uid): + # XXX: only shield the context sync step! with trio.CancelScope(shield=True): # indicate to child that we've locked stdio @@ -188,12 +189,22 @@ async def _hijack_stdin_relay_to_child( # wait for unlock pdb by child async with ctx.open_stream() as stream: - assert await stream.receive() == 'Unlock' + try: + assert await stream.receive() == 'pdb_unlock' + + except trio.BrokenResourceError: + # XXX: there may be a race with the portal teardown + # with the calling actor which we can safely ignore + # the alternative would be sending an ack message + # and allowing the client to wait for us to teardown + # first? + pass log.debug( f"TTY lock released, remote task: {task_name}:{subactor_uid}") log.debug(f"Actor {subactor_uid} RELEASED stdin hijack lock") + return "pdb_unlock_complete" async def _breakpoint(debug_func) -> None: @@ -233,8 +244,17 @@ async def _breakpoint(debug_func) -> None: # unblock local caller task_status.started() + # TODO: shielding currently can cause hangs... + # with trio.CancelScope(shield=True): + await _pdb_complete.wait() - await stream.send('Unlock') + await stream.send('pdb_unlock') + + # sync with callee termination + assert await ctx.result() == "pdb_unlock_complete" + + except tractor.ContextCancelled: + log.warning('Root actor cancelled debug lock') finally: log.debug(f"Exiting debugger for actor {actor}")