From 649779ee52e78d8b1bfee3a5c79128f2cf92d5e0 Mon Sep 17 00:00:00 2001 From: Jason Sherman Date: Fri, 26 May 2023 10:20:35 -0700 Subject: [PATCH] ./run_demo performance -c 1 --mediation --timing --trace-log Bug with prompt_toolkit and trace logging. Could only bump up the prompt_toolkit library so much since demos run on Python 3.6. However, I did attempt on 3.9 with the latest version of prompt_toolkit and setting an env var (see https://github.com/prompt-toolkit/python-prompt-toolkit/pull/898). This still did not correct the issue, so reverted to existing code and just handled the exception(s). The output of the trace log is being written to the console, my limited understanding of this would be that the trace log works, but also triggers the prompt_toolkit to do the same work and gets into conflict. Signed-off-by: Jason Sherman --- demo/runners/support/agent.py | 13 ++++++++++++- demo/runners/support/utils.py | 7 ++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/demo/runners/support/agent.py b/demo/runners/support/agent.py index 0aa7f995ed..45e3b317bd 100644 --- a/demo/runners/support/agent.py +++ b/demo/runners/support/agent.py @@ -662,7 +662,18 @@ def handle_output(self, *output, source: str = None, **kwargs): color = self.color or "fg:ansiblue" else: color = None - log_msg(*output, color=color, prefix=self.prefix_str, end=end, **kwargs) + try: + log_msg(*output, color=color, prefix=self.prefix_str, end=end, **kwargs) + except AssertionError as e: + if self.trace_enabled and self.trace_target == "log": + # when tracing to a log file, + # we hit an issue with the underlying prompt_toolkit. + # it attempts to output what is written by the log and can't find the + # correct terminal and throws an error. The trace log record does show + # in the terminal, so let's just ignore this error. + pass + else: + raise e def log(self, *msg, **kwargs): self.handle_output(*msg, **kwargs) diff --git a/demo/runners/support/utils.py b/demo/runners/support/utils.py index 63982d4437..0056686fbf 100644 --- a/demo/runners/support/utils.py +++ b/demo/runners/support/utils.py @@ -113,7 +113,12 @@ def output_reader(handle, callback, *args, **kwargs): for line in iter(handle.readline, b""): if not line: break - run_in_terminal(functools.partial(callback, line, *args)) + try: + run_in_terminal(functools.partial(callback, line, *args)) + except AssertionError as e: + # see comment in DemoAgent.handle_output + # trace log and prompt_toolkit do not get along... + pass def log_msg(*msg, color="fg:ansimagenta", **kwargs):