Repository: xonsh/xonsh

Issue:
xonsh freezes and uses 100% cpu after a simple command errors
Brand new installation of xonsh on macOS 10.13.4, no custom configuration.

```
~$ xonsh --version
xonsh/0.6.4
~$ xonsh
<welcome message elided>
russell@russell ~ $ $RAISE_SUBPROC_ERROR = True
russell@russell ~ $ ls file-that-does-not-exist
ls: file-that-does-not-exist: No such file or directory
xonsh: For full traceback set: $XONSH_SHOW_TRACEBACK = True
subprocess.CalledProcessError: Command '['ls', '-G', 'file-that-does-not-exist']' returned non-zero exit status 1.
```

xonsh does not return to a prompt, pressing Ctrl-C, Ctrl-D, Ctrl-Z, etc., all have no effect, and CPU usage goes to 100%.


Discussion:
Thanks for reporting @russelldavis - Can you pleas provide the output of the `xonfig` command in a new terminal? This will help us identify your system.
Sure, here it is:
```
+------------------+-----------------+
| xonsh            | 0.6.4           |
| Python           | 3.6.5           |
| PLY              | 3.9             |
| have readline    | True            |
| prompt toolkit   | None            |
| shell type       | readline        |
| pygments         | None            |
| on posix         | True            |
| on linux         | False           |
| on darwin        | True            |
| on windows       | False           |
| on cygwin        | False           |
| on msys2         | False           |
| is superuser     | False           |
| default encoding | utf-8           |
| xonsh encoding   | utf-8           |
| encoding errors  | surrogateescape |
+------------------+-----------------+
```
Hmm this is very strange.  You seem to be on a mac.  I cannot reproduce this issue on my linux box.  
Just tried on a mac I have access to, and it still didn't replicate this issue.  

```console
anthony@asmac ~ $ xonfig
+------------------+-----------------+
| xonsh            | 0.6.6           |
| Python           | 3.6.5           |
| PLY              | 3.11            |
| have readline    | True            |
| prompt toolkit   | 1.0.15          |
| shell type       | prompt_toolkit  |
| pygments         | 2.2.0           |
| on posix         | True            |
| on linux         | False           |
| on darwin        | True            |
| on windows       | False           |
| on cygwin        | False           |
| on msys2         | False           |
| is superuser     | False           |
| default encoding | utf-8           |
| xonsh encoding   | utf-8           |
| encoding errors  | surrogateescape |
+------------------+-----------------+
anthony@asmac ~ $ ls notafile
ls: notafile: No such file or directory
```

Could you provide more information about your system, such as your xonshrc an `xontrib list`?
I can sort of replicate it, but only if `$RAISE_SUBPROC_ERROR=True`

```
~ $ $RAISE_SUBPROC_ERROR=True
~ $ ls file-that-does-not-exist
xonsh: For full traceback set: $XONSH_SHOW_TRACEBACK = True
termios.error: (4, 'Interrupted system call')
```

And the error message scrolls infinitely and cannot be cancelled without closing the terminal session entirely.  This is on iTerm2 

```
+------------------+----------------------+
| xonsh            | 0.6.6                |
| Git SHA          | d87b7e5e             |
| Commit Date      | May 24 04:18:39 2018 |
| Python           | 3.6.5                |
| PLY              | 3.9                  |
| have readline    | True                 |
| prompt toolkit   | 1.0.15               |
| shell type       | prompt_toolkit       |
| pygments         | 2.2.0                |
| on posix         | True                 |
| on linux         | False                |
| on darwin        | True                 |
| on windows       | False                |
| on cygwin        | False                |
| on msys2         | False                |
| is superuser     | False                |
| default encoding | utf-8                |
| xonsh encoding   | utf-8                |
| encoding errors  | surrogateescape      |
+------------------+----------------------+
```
Facinating.  I got that on my mac. I was ssh'd in from Linux, so this ins't an iTerm issue :).  @russelldavis - can you confirm that you have `$RAISE_SUBPROC_ERROR=True`, please?
Here is the traceback I am seeing:

```
xonsh: To log full traceback to a file set: $XONSH_TRACEBACK_LOGFILE = <filename>
Traceback (most recent call last):
  File "/Users/anthony/miniconda/lib/python3.6/site-packages/xonsh/ptk/shortcuts.py", line 109, in prompt
    document = cli.run(reset_current_buffer=False)
  File "/Users/anthony/miniconda/lib/python3.6/site-packages/prompt_toolkit/interface.py", line 411, in run
    with self.input.raw_mode():
  File "/Users/anthony/miniconda/lib/python3.6/site-packages/prompt_toolkit/terminal/vt100_input.py", line 470, in __enter__
    termios.tcsetattr(self.fileno, termios.TCSANOW, newattr)
termios.error: (4, 'Interrupted system call')
```
He does have it set in his example blurb up top, FWIW.
I wonder if this will just go away when we move to prompt_toolkit 2.0?
That is a good hope, but here is the traceback I see with readline:

```console
anthony@asmac ~ $ ls notafile
ls: notafile: No such file or directory
xonsh: To log full traceback to a file set: $XONSH_TRACEBACK_LOGFILE = <filename>
Traceback (most recent call last):
  File "/Users/anthony/miniconda/lib/python3.6/site-packages/xonsh/base_shell.py", line 330, in default
    run_compiled_code(code, self.ctx, None, 'single')
  File "/Users/anthony/miniconda/lib/python3.6/site-packages/xonsh/codecache.py", line 68, in run_compiled_code
    func(code, glb, loc)
  File "<xonsh-code>", line 1, in <module>
  File "/Users/anthony/miniconda/lib/python3.6/site-packages/xonsh/built_ins.py", line 881, in subproc_captured_hiddenobject
    return run_subproc(cmds, captured='hiddenobject')
  File "/Users/anthony/miniconda/lib/python3.6/site-packages/xonsh/built_ins.py", line 844, in run_subproc
    command.end()
  File "/Users/anthony/miniconda/lib/python3.6/site-packages/xonsh/proc.py", line 2014, in end
    self._end(tee_output=tee_output)
  File "/Users/anthony/miniconda/lib/python3.6/site-packages/xonsh/proc.py", line 2033, in _end
    self._raise_subproc_error()
  File "/Users/anthony/miniconda/lib/python3.6/site-packages/xonsh/proc.py", line 2153, in _raise_subproc_error
    output=self.output)
subprocess.CalledProcessError: Command '['ls', '-G', 'notafile']' returned non-zero exit status 1.
```
I think the issue might be that `$RAISE_SUBPROC_ERROR` really shouldn't be True in interactive mode.
The whole point is to take out the process. And oddly, I can now reproduce in linux.

Resolving patch:
diff --git a/xonsh/proc.py b/xonsh/proc.py
--- a/xonsh/proc.py
+++ b/xonsh/proc.py
@@ -2149,8 +2149,12 @@ def _raise_subproc_error(self):
                 rtn is not None and
                 rtn > 0 and
                 builtins.__xonsh_env__.get('RAISE_SUBPROC_ERROR')):
-            raise subprocess.CalledProcessError(rtn, spec.cmd,
-                                                output=self.output)
+            try:
+                raise subprocess.CalledProcessError(rtn, spec.cmd,
+                                                    output=self.output)
+            finally:
+                # this is need to get a working terminal in interactive mode
+                self._return_terminal()
 
     #
     # Properties


Test patch: