From 1ae9c50fbbe1fd4387659246e1d7d79d98c2bc01 Mon Sep 17 00:00:00 2001 From: gaogaotiantian Date: Mon, 17 Jul 2023 11:43:43 -0700 Subject: [PATCH 1/4] Fixed exception handling for vizviewer --- src/viztracer/viewer.py | 12 +++++++++--- tests/test_viewer.py | 3 +++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/viztracer/viewer.py b/src/viztracer/viewer.py index c4b96fea..15d8ea08 100644 --- a/src/viztracer/viewer.py +++ b/src/viztracer/viewer.py @@ -14,6 +14,7 @@ import socketserver import subprocess import sys +import traceback import threading import time import urllib.parse @@ -298,9 +299,14 @@ def __init__( super().__init__(daemon=True) def run(self) -> None: - self.retcode = self.view() - # If it returns from view(), also set ready - self.ready.set() + try: + self.retcode = self.view() + except Exception as e: + self.retcode = 1 + traceback.print_exception(e) + finally: + # If it returns from view(), also set ready + self.ready.set() def view(self) -> int: # Get file data diff --git a/tests/test_viewer.py b/tests/test_viewer.py index 50a1d5a8..cdebb0fc 100644 --- a/tests/test_viewer.py +++ b/tests/test_viewer.py @@ -457,6 +457,9 @@ def test_directory_max_port(self): finally: shutil.rmtree(tmp_dir) + def test_exception(self): + self.template(["vizviewer", "--port", "-3", "cmdline_test.json"], success=False, expected_output_file=None) + def test_invalid(self): self.template(["vizviewer", "do_not_exist.json"], success=False, expected_output_file=None) self.template(["vizviewer", "README.md"], success=False, expected_output_file=None) From 823ec0b4b53c19b4043281ef1318badb2f18973a Mon Sep 17 00:00:00 2001 From: gaogaotiantian Date: Mon, 17 Jul 2023 11:51:12 -0700 Subject: [PATCH 2/4] Use a backward compatible print --- src/viztracer/viewer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/viztracer/viewer.py b/src/viztracer/viewer.py index 15d8ea08..d7ae8c2d 100644 --- a/src/viztracer/viewer.py +++ b/src/viztracer/viewer.py @@ -303,7 +303,7 @@ def run(self) -> None: self.retcode = self.view() except Exception as e: self.retcode = 1 - traceback.print_exception(e) + traceback.print_exc() finally: # If it returns from view(), also set ready self.ready.set() From e1554c807dd9d4346785445b232fcdd3d2b2fd00 Mon Sep 17 00:00:00 2001 From: gaogaotiantian Date: Mon, 17 Jul 2023 12:12:53 -0700 Subject: [PATCH 3/4] Remove unused variable --- src/viztracer/viewer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/viztracer/viewer.py b/src/viztracer/viewer.py index d7ae8c2d..f66c904f 100644 --- a/src/viztracer/viewer.py +++ b/src/viztracer/viewer.py @@ -301,7 +301,7 @@ def __init__( def run(self) -> None: try: self.retcode = self.view() - except Exception as e: + except Exception: self.retcode = 1 traceback.print_exc() finally: From 5742759d7bcb9640374e36c99a4cfb6889acd715 Mon Sep 17 00:00:00 2001 From: gaogaotiantian Date: Mon, 17 Jul 2023 13:15:11 -0700 Subject: [PATCH 4/4] Update tests --- tests/cmdline_tmpl.py | 11 ++++++----- tests/test_viewer.py | 4 +++- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/tests/cmdline_tmpl.py b/tests/cmdline_tmpl.py index d4419d33..2e53ef71 100644 --- a/tests/cmdline_tmpl.py +++ b/tests/cmdline_tmpl.py @@ -118,20 +118,21 @@ def template(self, logging.error(f"stdout: {e.stdout}") logging.error(f"stderr: {e.stderr}") raise e - if not (success ^ (result.returncode != 0)): + expected = (success ^ (result.returncode != 0)) + if not expected: logging.error(f"return code: {result.returncode}") logging.error(f"stdout:\n{result.stdout.decode('utf-8')}") logging.error(f"stderr:\n{result.stderr.decode('utf-8')}") - self.assertTrue(success ^ (result.returncode != 0)) - if success: - if expected_output_file: + self.assertTrue(expected) + if expected: + if success and expected_output_file: if type(expected_output_file) is list: for f in expected_output_file: self.assertFileExists(f) elif type(expected_output_file) is str: self.assertFileExists(expected_output_file) - if expected_entries: + if success and expected_entries: assert (type(expected_output_file) is str and expected_output_file.split(".")[-1] == "json") with open(expected_output_file) as f: data = json.load(f) diff --git a/tests/test_viewer.py b/tests/test_viewer.py index cdebb0fc..a6eae713 100644 --- a/tests/test_viewer.py +++ b/tests/test_viewer.py @@ -458,7 +458,9 @@ def test_directory_max_port(self): shutil.rmtree(tmp_dir) def test_exception(self): - self.template(["vizviewer", "--port", "-3", "cmdline_test.json"], success=False, expected_output_file=None) + test_data_dir = os.path.join(os.path.dirname(__file__), "data") + self.template(["vizviewer", "--port", "-3", os.path.join(test_data_dir, "fib.json")], + success=False, expected_output_file=None, expected_stderr=".*Traceback.*") def test_invalid(self): self.template(["vizviewer", "do_not_exist.json"], success=False, expected_output_file=None)