From bd686a442ef41e118432f2d21034d92830948183 Mon Sep 17 00:00:00 2001 From: Pratham Gupta <87975651+PrathamGupta06@users.noreply.github.com> Date: Fri, 2 Aug 2024 14:13:11 +0530 Subject: [PATCH] Shutdown taipy gracefully without long traceback (#1595) * Handle Keyboard Interrupt Error * Handle KeyboardInterrupt with gevent --- taipy/_cli/_run_cli.py | 13 ++++++++----- taipy/gui/server.py | 8 ++++++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/taipy/_cli/_run_cli.py b/taipy/_cli/_run_cli.py index 115ce92e4a..5811f221d2 100644 --- a/taipy/_cli/_run_cli.py +++ b/taipy/_cli/_run_cli.py @@ -54,10 +54,13 @@ def handle_command(cls): taipy_args = [f"--taipy-{arg[2:]}" if arg.startswith("--") else arg for arg in all_args] - subprocess.run( - [sys.executable, args.application_main_file, *(external_args + taipy_args)], - stdout=sys.stdout, - stderr=sys.stdout, - ) + try: + subprocess.run( + [sys.executable, args.application_main_file, *(external_args + taipy_args)], + stdout=sys.stdout, + stderr=sys.stdout, + ) + except KeyboardInterrupt: + pass sys.exit(0) diff --git a/taipy/gui/server.py b/taipy/gui/server.py index f40fc9538f..e6493731fc 100644 --- a/taipy/gui/server.py +++ b/taipy/gui/server.py @@ -256,8 +256,9 @@ def _get_async_mode(self) -> str: def _apply_patch(self): if self._get_async_mode() == "gevent" and util.find_spec("gevent"): - from gevent import monkey + from gevent import get_hub, monkey + get_hub().NOT_ERROR += (KeyboardInterrupt, ) if not monkey.is_module_patched("time"): monkey.patch_time() if self._get_async_mode() == "eventlet" and util.find_spec("eventlet"): @@ -318,7 +319,10 @@ def run(self, host, port, debug, use_reloader, flask_log, run_in_thread, allow_u # flask-socketio specific conditions for 'allow_unsafe_werkzeug' parameters to be popped out of kwargs if self._get_async_mode() == "threading" and (not sys.stdin or not sys.stdin.isatty()): run_config = {**run_config, "allow_unsafe_werkzeug": allow_unsafe_werkzeug} - self._ws.run(**run_config) + try: + self._ws.run(**run_config) + except KeyboardInterrupt: + pass def stop_thread(self): if hasattr(self, "_thread") and self._thread.is_alive() and self._is_running: