Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed exception handling for vizviewer #354

Merged
merged 4 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/viztracer/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import socketserver
import subprocess
import sys
import traceback
import threading
import time
import urllib.parse
Expand Down Expand Up @@ -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:
self.retcode = 1
traceback.print_exc()
finally:
# If it returns from view(), also set ready
self.ready.set()

def view(self) -> int:
# Get file data
Expand Down
11 changes: 6 additions & 5 deletions tests/cmdline_tmpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,11 @@ def test_directory_max_port(self):
finally:
shutil.rmtree(tmp_dir)

def test_exception(self):
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)
self.template(["vizviewer", "README.md"], success=False, expected_output_file=None)
Expand Down