Repository: xonsh/xonsh

Issue:
bare exception in xonsh when executing process via sudo
When running a process via 'sudo', an exception is raised sometimes:

```python-traceback
xonsh: To log full traceback to a file set: $XONSH_TRACEBACK_LOGFILE = <filename>
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/xonsh/base_shell.py", line 360, in default
    run_compiled_code(code, self.ctx, None, "single")
  File "/usr/local/lib/python3.6/dist-packages/xonsh/codecache.py", line 67, in run_compiled_code
    func(code, glb, loc)
  File "./foo.xsh", line 1, in <module>
  File "./foo.xsh", line 21, in destroy_containers
    sudo docker container stop @(container.hash)
  File "/usr/local/lib/python3.6/dist-packages/xonsh/built_ins.py", line 946, in subproc_captured_hiddenobject
    return run_subproc(cmds, captured="hiddenobject")
  File "/usr/local/lib/python3.6/dist-packages/xonsh/built_ins.py", line 898, in run_subproc
    pause_call_resume(proc, builtins.__xonsh__.shell.settitle)
  File "/usr/local/lib/python3.6/dist-packages/xonsh/proc.py", line 2416, in pause_call_resume
    p.send_signal(signal.SIGSTOP)
  File "/usr/lib/python3.6/subprocess.py", line 1580, in send_signal
    os.kill(self.pid, sig)
PermissionError: [Errno 1] Operation not permitted

This does not happen every time, but it's reasonably consistent.  Race?

```

The actual code run is `sudo docker container stop @(container.hash)`, where `container.hash` is just a namedtuple containing a text string.

This is on an Amazon Ubuntu AMI, t2.micro.

## xonfig
```
+------------------+---------------------+
| xonsh            | 0.8.0               |
| Git SHA          | aca294b8            |
| Commit Date      | Oct 7 09:53:16 2018 |
| Python           | 3.6.6               |
| PLY              | 3.9                 |
| have readline    | True                |
| prompt toolkit   | None                |
| shell type       | readline            |
| pygments         | None                |
| on posix         | True                |
| on linux         | True                |
| distro           | Ubuntu              |
| on darwin        | False               |
| on windows       | False               |
| on cygwin        | False               |
| on msys2         | False               |
| is superuser     | False               |
| default encoding | utf-8               |
| xonsh encoding   | utf-8               |
| encoding errors  | surrogateescape     |
+------------------+---------------------+
```

## Expected Behavior
An exception should only be raised if my code is wrong.

## Current Behavior
An exception is raised for some other reason, shown in the above traceback.

## Steps to Reproduce
This is the exact code:
```python
from collections import namedtuple

Container = namedtuple('Container', 'hash, name')
def containers(name=None):
    containers = [
        Container(line.split()[0],
                  line.split()[-1],
        ) 
        for line in $(sudo docker container ls).split('\n') 
        if line.strip()
    ][1:]
    if name is not None:
        for container in containers:
            if container.name == name: return container
        return None
    return containers

def destroy_containers():
    for container in containers():
        sudo docker container stop @(container.hash)
    sudo docker container prune
```


Discussion:
This also occurs when just executing a command on the command-line.

```python-traceback
$ sudo docker container stop foo
xonsh: To log full traceback to a file set: $XONSH_TRACEBACK_LOGFILE = <filename>
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/xonsh/base_shell.py", line 360, in default
    run_compiled_code(code, self.ctx, None, "single")
  File "/usr/local/lib/python3.6/dist-packages/xonsh/codecache.py", line 67, in run_compiled_code
    func(code, glb, loc)
  File "<xonsh-code>", line 1, in <module>
  File "/usr/local/lib/python3.6/dist-packages/xonsh/built_ins.py", line 946, in subproc_captured_hiddenobject
    return run_subproc(cmds, captured="hiddenobject")
  File "/usr/local/lib/python3.6/dist-packages/xonsh/built_ins.py", line 898, in run_subproc
    pause_call_resume(proc, builtins.__xonsh__.shell.settitle)
  File "/usr/local/lib/python3.6/dist-packages/xonsh/proc.py", line 2416, in pause_call_resume
    p.send_signal(signal.SIGSTOP)
  File "/usr/lib/python3.6/subprocess.py", line 1580, in send_signal
    os.kill(self.pid, sig)
PermissionError: [Errno 1] Operation not permitted
```

In either case (in a function, or on the cli) this freezes the XONSH instance, which must be sent a `kill -9`.


Resolving patch:
diff --git a/xonsh/proc.py b/xonsh/proc.py
--- a/xonsh/proc.py
+++ b/xonsh/proc.py
@@ -2413,7 +2413,10 @@ def pause_call_resume(p, f, *args, **kwargs):
         hasattr(p, "send_signal") and ON_POSIX and not ON_MSYS and not ON_CYGWIN
     )
     if can_send_signal:
-        p.send_signal(signal.SIGSTOP)
+        try:
+            p.send_signal(signal.SIGSTOP)
+        except PermissionError:
+            pass
     try:
         f(*args, **kwargs)
     except Exception:


Test patch: