From 497fa72c967399e89744c06e2e628bda3f8fcea3 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Mon, 16 Nov 2020 00:01:21 -0500 Subject: [PATCH] Add a SIGINT handler that kills the process tree We're not actually using this but it's for reference if we do end up needing it. The std lib's `pdb` internals override SIGINT handling whenever one enters the debugger repl. Force a handler that kills the tree if SIGINT is triggered from the root actor, otherwise ignore it since supervised children should be managed already. This resolves an issue with guest mode where `pdb` causes SIGINTs to be swallowed resulting in the host loop never terminating the process tree. --- tractor/_debug.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tractor/_debug.py b/tractor/_debug.py index 4fc7495..7897e00 100644 --- a/tractor/_debug.py +++ b/tractor/_debug.py @@ -196,6 +196,22 @@ async def _acquire_debug_lock( log.debug(f"TTY lock released, remote task: {task_name}:{uid}") +def handler(signum, frame, *args): + """Specialized debugger compatible SIGINT handler. + + In childred we always ignore to avoid deadlocks since cancellation + should always be managed by the parent supervising actor. The root + is always cancelled on ctrl-c. + """ + if is_root_process(): + tractor.current_actor().cancel_soon() + else: + print( + "tractor ignores SIGINT while in debug mode\n" + "If you have a special need for it please open an issue.\n" + ) + + @tractor.context async def _hijack_stdin_for_child(