Skip to content

Commit

Permalink
Improve interrupt handling and send signal to current loop
Browse files Browse the repository at this point in the history
  • Loading branch information
fepitre committed Aug 20, 2024
1 parent 234bd95 commit 162474b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
44 changes: 31 additions & 13 deletions qubesbuilder/cli/cli_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"""
QubesBuilder command-line interface - base module.
"""

import asyncio
import signal
import sys
import traceback
from typing import Callable, List
Expand Down Expand Up @@ -61,21 +62,38 @@ def __init__(self, *args, **kwargs):
self.list_commands = self.list_commands_for_help # type: ignore

def __call__(self, *args, **kwargs):
loop = asyncio.get_event_loop()

def _handle_interrupt():
tasks = asyncio.all_tasks(loop)
for task in tasks:
task.cancel()

loop.add_signal_handler(signal.SIGINT, _handle_interrupt)

# default return code
rc = 0

try:
return self.main(*args, **kwargs)
self.main(*args, standalone_mode=False, **kwargs)
except Exception as exc:
QubesBuilderLogger.error(f"An error occurred: {str(exc)}")
if self.debug is True:
formatted_traceback = "".join(traceback.format_exception(exc))
QubesBuilderLogger.error(
"\n" + formatted_traceback.rstrip("\n")
)

if isinstance(exc, click.ClickException):
# pylint: disable=no-member
sys.exit(exc.exit_code)
rc = 1
if isinstance(exc, click.Abort) or "Signals.SIGINT" in str(exc):
QubesBuilderLogger.warning(f"Interrupting...")
else:
sys.exit(1)
QubesBuilderLogger.error(f"An error occurred: {str(exc)}")
if self.debug is True:
formatted_traceback = "".join(
traceback.format_exception(exc)
)
QubesBuilderLogger.error(
"\n" + formatted_traceback.rstrip("\n")
)
if isinstance(exc, click.ClickException):
# pylint: disable=no-member
rc = exc.exit_code
finally:
sys.exit(rc)

def get_command(self, ctx, cmd_name):
rv = click.Group.get_command(self, ctx, cmd_name)
Expand Down
2 changes: 1 addition & 1 deletion qubesbuilder/executors/qubes.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ def run( # type: ignore
)
continue
raise e
except ExecutorError as e:
except (subprocess.CalledProcessError, ExecutorError) as e:
if dispvm and self._clean_on_error:
self.cleanup(dispvm)
raise e
Expand Down

0 comments on commit 162474b

Please sign in to comment.