Repository: hyperledger/aries-cloudagent-python

Issue:
Error running performance demo with “—trace-log” parameter set
When running the performance demo with:

`./run_demo performance -c 1 --mediation --timing --trace-log`

Many errors were generated like the following. Likely to do with the `—trace-log` parameter as without that, the run works correctly.

```
AssertionError
Exception in default exception handler
Traceback (most recent call last):
  File "/home/indy/.pyenv/versions/3.6.13/lib/python3.6/site-packages/prompt_toolkit/eventloop/coroutine.py", line 94, in step_next
    new_f = coroutine.send(f.result())
  File "/home/indy/.pyenv/versions/3.6.13/lib/python3.6/site-packages/prompt_toolkit/application/run_in_terminal.py", line 104, in _run_in_t
    result = yield From(async_func())
  File "/home/indy/.pyenv/versions/3.6.13/lib/python3.6/site-packages/prompt_toolkit/application/run_in_terminal.py", line 47, in async_func
    result = func()
  File "/home/indy/demo/runners/support/agent.py", line 665, in handle_output
    log_msg(*output, color=color, prefix=self.prefix_str, end=end, **kwargs)
  File "/home/indy/demo/runners/support/utils.py", line 120, in log_msg
    run_in_terminal(lambda: print_ext(*msg, color=color, **kwargs))
  File "/home/indy/.pyenv/versions/3.6.13/lib/python3.6/site-packages/prompt_toolkit/application/run_in_terminal.py", line 50, in run_in_terminal
    return run_coroutine_in_terminal(async_func, render_cli_done=render_cli_done)
  File "/home/indy/.pyenv/versions/3.6.13/lib/python3.6/site-packages/prompt_toolkit/application/run_in_terminal.py", line 71, in run_coroutine_in_terminal
    assert app._is_running
AssertionError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/indy/.pyenv/versions/3.6.13/lib/python3.6/site-packages/prompt_toolkit/eventloop/base.py", line 132, in call_exception_handler
    self._exception_handler(context)
  File "/home/indy/.pyenv/versions/3.6.13/lib/python3.6/site-packages/prompt_toolkit/application/application.py", line 729, in handle_exception
    run_coroutine_in_terminal(print_exception)
  File "/home/indy/.pyenv/versions/3.6.13/lib/python3.6/site-packages/prompt_toolkit/application/run_in_terminal.py", line 71, in run_coroutine_in_terminal
    assert app._is_running
```


Discussion:
@usingtechnology — adding this to your list since this is related to what you are working on right now. Worst case you ask @ianco about it when you are talking with him.
@swcurran I wonder if this is a timing issue ...  the agents seem to all start up ok, but the errors are generated in the logging to the terminal

Any idea what to do with it @ianco ?  If the agents are starting up fine, but we are getting errors, do we just document that the errors should be ignored?  Or do we have @usingtechnology dig into see what is up?

I wondered if something other than just saying “—trace-log” is needed — e.g. is there an environment variable needed?  Is this happening because of permissions about where ACA-Py is trying to put the log (e.g. the problem with docker creating the folder with root:root ownership so that a running ACA-Py can’t write to the file).

The only reason I added this to @usingtechnology ’s list is because it is so close (I think!) to the tracing work he is doing. If that is not the case, we can remove it from his list.

Advice welcome! :-)
quick update. running against https://github.com/hyperledger/aries-cloudagent-python/pull/2075 which brings in the mediation fix, but still encounter errors as above with `--trace-log`.

will dig in and tackle this now.

Resolving patch:
diff --git a/demo/runners/support/agent.py b/demo/runners/support/agent.py
--- a/demo/runners/support/agent.py
+++ b/demo/runners/support/agent.py
@@ -663,7 +663,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
--- 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):


Test patch: